我是一名有用的助手,可以为您翻译文本。
我尝试将最后一个
我在使用Unity时遇到了问题,以下是代码:
错误信息:
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;
时仍然会出现错误。
colorSequence
变量在哪里被初始化了? - clcto