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

时间:2013-10-17 21:08:53

标签: c# arrays indexing elements

道歉,如果在其他地方得到解答,但我相信我的问题是非常具体的,我没有丝毫的线索,根据过去的python经验,为什么这段代码会返回错误:

IndexOutOfRangeException: Array index is out of range.
(wrapper stelemref) object:stelemref (object,intptr,object)
MissionGen.Start () (at Assets/Scripts/MissionGen.cs:59)

我目前正在Unity引擎中学习C#,并创建了一个小型实验脚本,旨在使用OOP生成任务,从阵列中选择目标名称 - 但显然,我不知道为什么会赢得这个'工作。我相信python中的等效错误只发生在我试图引用不存在的数组元素时。虽然这也可能就是这种情况,但由于编写代码的方式,我不明白为什么。无论如何,这是完整的脚本!:

// Simple mission generation script, using arrays and object orientated programming. Designed to be
//  upgraded at a later date. 

using UnityEngine;
using System.Collections;

// Mission generation template

public class MissionGen : MonoBehaviour {

    public string[] t_list_t = new string[] {"Dave", "Johnson", "Hadaki", "Tim", "Timothy", "Chris Roberts"};
    public string[] t_list_c_col = new string[] {"Blue", "Yellow", "Green", "Black", "Orange", "Purple"};
    public string[] t_list_h_col = new string[] {"Black", "Green", "Orange", "Blue", "Red", "Brown"};

    public class MissionTemplate {

    // Mission properties

        public int id;
        public string t_name;
        public string t_coat;
        public string t_hat;
        public string type;
        public int reward1;
        public string reward2;

    // A method, for displaying the attributes of an individual mission.

    public void ShowMission () {
        print ("MISSION ID: " + id);
        print ("TARGET NAME: " + t_name);
        print ("TARGET COAT COLOUR: " + t_coat);
        print ("TARGET HAT COLOUR: " + t_hat);
        print ("MISSION TYPE: " + type);
        print ("REWARD 1 (Money): " + reward1);
        print ("REWARD 2 (Item): " + reward2);
    }
}

// Mission array generation. Change the array range to generate more missions for use in the game.

    void Start() {

        // Change this variable to decide how many missions to generate:

        int gen = 50;

        // Change the above variable to designate the number of missions to generate.

        MissionTemplate[] MISSION = new MissionTemplate[gen];

        for (int i = 1; i <= gen; i++)
        {   

        int t_pick = Random.Range (0,5);
        int t_coat_col = Random.Range (0,5);
        int t_hat_col = Random.Range (0,5);

        MISSION[i] = new MissionTemplate();

        MISSION[i].id = i;
        MISSION[i].t_name = t_list_t[t_pick];
        MISSION[i].t_coat = t_list_c_col[t_coat_col];
        MISSION[i].t_hat = t_list_h_col[t_hat_col];
        MISSION[i].type = "Assassination";
        MISSION[i].reward1 = 0;
        MISSION[i].reward2 = "";
        }

        for (int i = 1; i <= gen; i++)
        {
        MISSION[i].ShowMission();   
        }
    }
}

作为细分,我相信以下代码之间会出现问题:

    for (int i = 1; i <= gen; i++)
    {   

    int t_pick = Random.Range (0,5);
    int t_coat_col = Random.Range (0,5);
    int t_hat_col = Random.Range (0,5);

    MISSION[i] = new MissionTemplate();

    MISSION[i].id = i;
    MISSION[i].t_name = t_list_t[t_pick];
    MISSION[i].t_coat = t_list_c_col[t_coat_col];
    MISSION[i].t_hat = t_list_h_col[t_hat_col];
    MISSION[i].type = "Assassination";
    MISSION[i].reward1 = 0;
    MISSION[i].reward2 = "";
    }

    for (int i = 1; i <= gen; i++)
    {
    MISSION[i].ShowMission();   
    }
}

}

public class MissionGen : MonoBehaviour {

    public string[] t_list_t = new string[] {"Dave", "Johnson", "Hadaki", "Tim", "Timothy", "Chris Roberts"};
    public string[] t_list_c_col = new string[] {"Blue", "Yellow", "Green", "Black", "Orange", "Purple"};
    public string[] t_list_h_col = new string[] {"Black", "Green", "Orange", "Blue", "Red", "Brown"};

非常感谢任何帮助!

谢谢,

1 个答案:

答案 0 :(得分:3)

这是问题所在:

MissionTemplate[] MISSION = new MissionTemplate[gen];
for (int i = 1; i <= gen; i++)
{
    ...
    MISSION[i] = new MissionTemplate();

除了违反正常的命名约定外,C#中的数组都是基于0的 - 例如,长度为5的数组的有效索引是0,1,2,3和4.

所以你的for循环应该是:

for (int i = 0; i < gen; i++)

或更“明显正确”(IMO):

for (int i = 0; i < MISSION.Length; i++)

这使得任何阅读代码的人都明白你在MISSION数组的范围内,而没有阅读数组创建语句。