Apologies if this is answered somewhere else but I believe my problem is very specific and I haven't got the slightest clue, based on past experience in python, why this code returns the error:
IndexOutOfRangeException: Array index is out of range.
(wrapper stelemref) object:stelemref (object,intptr,object)
MissionGen.Start () (at Assets/Scripts/MissionGen.cs:59)
I am currently in the process of learning C# in the Unity engine, and have created a small class experimentation script designed to generate missions using OOP, selecting target names from an array - but obviously, I have no idea why this won't work. As I believe the equivalent error in python only occurs when I have tried to refrence an array element that doesn't exist. And though this is probably also the case here, due to the way the code is written, I don't understand why. Anyway, here is the full script!:
// 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();
}
}
}
As a breakdown, I believe the problems arise between the following code:
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();
}
}
}
And:
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"};
Any help would be much appreciated!
Thanks,
This is the problem:
MissionTemplate[] MISSION = new MissionTemplate[gen];
for (int i = 1; i <= gen; i++)
{
...
MISSION[i] = new MissionTemplate();
Aside from violating normal naming conventions, arrays in C# are 0-based - for example, the valid indexes for an array of length 5 are 0, 1, 2, 3 and 4.
So your for
loop should be:
for (int i = 0; i < gen; i++)
Or more "obviously correctly" (IMO):
for (int i = 0; i < MISSION.Length; i++)
That makes it obvious to anyone reading the code that you're staying within the bounds of the MISSION
array, without having read the array creation statement.
See more on this question at Stackoverflow