- Home /
Argument out of range - Generic list (JS)
I'm using a class to organize many arrays into an object. Then using that object to tell my game what to do at certain stages. This error is thrown ArgumentOutOfRangeException: Argument is out of range and I have no idea why. Here's my code for accessing the list:
import System.Collections.Generic;
var skin: GUISkin;
function OnGUI() {
GUI.skin=skin;
var item = Stage.Stages[0];
}
And this is my code for the stage class:
import System.Collections.Generic;
class Stage
{
var controlArray: String[];
var taskName: String[];
var taskStatus: Texture2D[];
var PC: boolean;
var arrowArray: GameObject[];
}
//what a stage object might look like
var Stage16 = new Stage();
Stage16.controlArray[0] = "";
Stage16.taskName[0] = "findTheWaterCooler";
Stage16.taskName[1] = "returnToYourDesk";
Stage16.taskName[2] = "dropToTheGround";
Stage16.taskName[3] = "coverUnderTheDesk";
Stage16.taskName[4] = "holdDuringTheEarthquake";
Stage16.taskName[5] = "dustYourselfOff";
Stage16.taskStatus[0] = Resources.Load("check_box_checked");
Stage16.taskStatus[1] = Resources.Load("check_box_failed");
Stage16.taskStatus[2] = Resources.Load("check_box_checked");
Stage16.taskStatus[3] = Resources.Load("check_box_checked");
Stage16.taskStatus[4] = Resources.Load("check_box_checked");
Stage16.taskStatus[5] = Resources.Load("check_box_empty");
Stage16.PC = false;
Stage16.arrowArray[0] = null;
static var Stages : List.<Stage> = new List.<Stage>(21);
Stages[0] = Stage0;
Stages[1] = Stage1;
Stages[2] = Stage2;
.
.
//and so on
I don't understand why its giving me a index out of range error?? Please help!
what is the full text of the error? What argument is it saying is out of range?
ArgumentOutOfRangeException: Argument is out of range. Parameter name: index System.Collections.Generic.List`1[Stage].get_Item (Int32 index) (at /Applications/buildAgent/work/c514da0c8183631c/mcs/class/corlib/System.Collections.Generic/List.cs:633) tasks.OnGUI () (at Assets/Scripts/tasks.js:32)
Answer by perchik · Sep 19, 2013 at 07:32 PM
I'm not as familiar with Javascript, but it seems that you'd have to initialize the array before accessing it:
Ie, doing this,
var taskName: String[]
and then this:
Stage16.taskName[0] = "findTheWaterCooler";
Stage16.taskName[1] = "returnToYourDesk";
Stage16.taskName[2] = "dropToTheGround";
Stage16.taskName[3] = "coverUnderTheDesk";
Stage16.taskName[4] = "holdDuringTheEarthquake";
Stage16.taskName[5] = "dustYourselfOff";
seems like a problem. I think you have to initialize it first:
taskName = new String[6]
did this
var Stage16 = new Stage();
var controlArray: String[];
var taskName: String[];
var taskStatus: Texture2D[];
var PC: boolean;
var arrowArray: GameObject[];
Stage16.controlArray[0] = "";
Stage16.taskName[0] = "findTheWaterCooler";
Stage16.taskName[1] = "returnToYourDesk";
Stage16.taskName[2] = "dropToTheGround";
Stage16.taskName[3] = "coverUnderTheDesk";
Stage16.taskName[4] = "holdDuringTheEarthquake";
Stage16.taskName[5] = "dustYourselfOff";
Stage16.taskStatus[0] = Resources.Load("check_box_checked");
Stage16.taskStatus[1] = Resources.Load("check_box_failed");
Stage16.taskStatus[2] = Resources.Load("check_box_checked");
Stage16.taskStatus[3] = Resources.Load("check_box_checked");
Stage16.taskStatus[4] = Resources.Load("check_box_checked");
Stage16.taskStatus[5] = Resources.Load("check_box_empty");
Stage16.PC = false;
Stage16.arrowArray[0] = null;
and got this error: Assets/Scripts/Stage.js(172,5): BCE0089: Type 'Stage' already has a definition for 'controlArray'. for each new var
Answer by DaveA · Sep 19, 2013 at 07:34 PM
Try breaking this
static var Stages : List. = new List.(21);
up into this:
static var Stages : List.<Stage>;
function Start()
{
Stages = new List.<Stage>(21);
}
then i get a NullReferenceException: Object reference not set to an instance of an object
What perchik said too. But don't use 'new' outside the scope of a function in general.