2013-10-17 63 views
0

道歉,如果这是回答其他地方,但我相信我的问题是非常具体的,我没有丝毫的线索,根据过去的经验在Python中,为什么这个代码返回错误:统一4 C# - IndexOutOfRangeException:数组索引超出范围

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

我目前在学习C#中的Unity引擎的过程中,并创造了设计使用任务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"}; 

任何帮助将不胜感激!

谢谢,

回答

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数组的边界内的代码,而不必阅读阵列创建语句。

+0

感谢您的快速响应!在我的脑海中有些东西挥之不去,可能是我想创造50个元素的方式。 (如果这就是你的意思)。至于数组编号系统,我知道它从0开始 - 我从C#开始非常糟糕:) - 但非常感谢您的回复!完美工作。 – WhiteRose