Unity C#-数组索引超出范围

 活跃的爱味儿县_454 发布于 2022-12-09 16:59

我在统一方面遇到麻烦,这是代码

错误信息:

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;
        }
    }
}

我试图将最后一个更改ifif (!colorSequence[i].Equals(null)),或者 if (colorSequence[i] == null)发生相同的错误。即使删除此内容if,当我尝试填充时也会发生错误colorSequence[i] = numRandom;

撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有