【发布时间】:2017-09-09 09:46:15
【问题描述】:
IndexOutOfRangeException:数组索引超出范围。 WordScramble.ShowScramble (Int32 index, Int32 index2) (at Assets/Word Sramble/WordScramble.cs:210) WordScramble.Start () (at Assets/Word Scramble/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());
}
【问题讨论】:
-
请解释问题而不是转储代码/异常消息
-
条件
(index > words.Length - 1) && (index2 > questions.Length - 1)只有当两个索引都大于它们各自的长度时才为真,所以很明显你可以将一个无效的索引传递给数组。 -
请不要通过破坏您的帖子为他人增加工作量。通过在 Stack Exchange 网络上发帖,您已在 CC BY-SA 4.0 license 下授予 Stack Exchange 分发该内容的不可撤销的权利(即无论您未来的选择如何)。根据 Stack Exchange 政策,帖子的非破坏版本是分发的版本。因此,任何破坏行为都将被撤销。如果您想了解更多关于删除帖子的信息,请参阅:How does deleting work?
标签: c# arrays unity3d indexing indexoutofrangeexception