我在统一方面遇到麻烦,这是代码
错误信息:
IndexOutOfRangeException: Array index is out of range. Sequence.fillSequenceArray () (at Assets/Scripts/Sequence.cs:43) Sequence.Start () (at Assets/Scripts/Sequence.cs:23)
码:
public int[] colorSequence = new int[100]; public int level = 2; // Use this for initialization void Start () { anim = GetComponent("Animator") as Animator; fillSequenceArray (); showArray (); // just to know } // Update is called once per frame void Update () { } public void showArray(){ for (int i = 0; i < colorSequence.Length; i++) { Debug.Log ("Position " + i + ":" + colorSequence[i]); } } public void fillSequenceArray(){ for (int i = 0; i < level; i++) { int numRandom = Random.Range (0, 3); if (colorSequence[i] == 0) { colorSequence[i] = numRandom; } } }
我试图将最后一个更改if
为if (!colorSequence[i].Equals(null))
,或者
if (colorSequence[i] == null)
发生相同的错误。即使删除此内容if
,当我尝试填充时也会发生错误colorSequence[i] = numRandom;