IndexOutOfRangeException:数组索引超出范围。 WordScramble.ShowScramble(Int32索引,Int32索引2)(在Assets / Word Sramble / WordScramble.cs:210) WordScramble.Start()(在Assets / Word Sramble / WordScramble.cs:134)
public void ShowScramble(int index, int index2)
{
textObjects.Clear ();
charObjects.Clear ();
foreach (Transform child in container)
{
Destroy (child.gameObject);
}
//WORDS FINISHED
//SHOW RESULT SCREEN
if ((index > words.Length - 1) && (index2 > questions.Length - 1))
{
result.ShowResult();
wordCanvas.SetActive(false);
//Debug.Log ("index out of range, please enter range between 0-" + (words.Length - 1).ToString());
return;
}
char[] chars = words [index].GetString ().ToCharArray ();
char[] chars2 = questions [index2].GetString ().ToCharArray ();
foreach (char c in chars)
{
TextObject clone2 = Instantiate (prefabQstn.gameObject).GetComponent<TextObject> ();
CharObject clone = Instantiate (prefab.gameObject).GetComponent<CharObject> ();
clone.transform.SetParent (container);
clone2.transform.SetParent (containerQstn);
textObjects.Add (clone2.Init (c));
charObjects.Add (clone.Init (c));
}
currentQstn = index2;
currentWord = index;
StartCoroutine (TimeLimit());
}
答案 0 :(得分:1)
索引检查未正确完成。需要进行OR操作。否则两个索引都必须超出范围才能满足条件。
if ((index > words.Length - 1) || (index2 > questions.Length - 1))
包含负数测试可能是个好主意:
if ((index > words.Length - 1) || (index2 > questions.Length - 1) || index < 0 || index2 < 0)
您未在以下代码中使用chars2
数组,因此请考虑正确更改代码。但我想这一切都取决于你希望代码做什么。