0

抱歉,如果在其他地方回答了这个问题,但我相信我的问题非常具体,我没有丝毫线索,根据过去的 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"};

任何帮助将非常感激!

谢谢,

4

1 回答 1

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数组的范围内,而无需阅读数组创建语句。

于 2013-10-17T21:11:08.553 回答