- Home /
JS Argument out of range
Getting this error: ArgumentOutOfRangeException: Argument is out of range. Parameter name: index
Been trying to get this to work for a few days now. Heres my code: This is my Stage class
import System.Collections.Generic;
class Stage
{
var controlArray: String[];
var taskName: String[];
var taskStatus: Texture2D[];
var PC: boolean;
var arrowArray: GameObject[];
}
and this is my script that handles actually putting it up on the GUI
static var Stages : List.<Stage> = new List.<Stage>(22);
function Start() {
var Stage0 = new Stage();
Stage0.controlArray = new String[2];
Stage0.arrowArray = new GameObject[4];
Stage0.controlArray[0] = "";
Stage0.PC = false;
Stage0.arrowArray[0] = null;
//...truncated
var Stage16 = new Stage();
Stage16.controlArray = new String[2];
Stage16.taskName = new String[6];
Stage16.taskStatus = new Texture2D[6];
Stage16.arrowArray = new GameObject[4];
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;
Stages[0] = Stage0;
//...truncated
Stages[21] = Stage21;
}
function OnGUI() {
GUI.skin=skin;
for (var i: int = 0; i < 22; i++)
{
if (GC.GetComponent(NewGC).stage==i) {
Debug.Log(Stages[i].taskName[0]);
if ((Stages[i].taskName[0]!=null)&&(Stages[i].taskStatus[0]!=null)) {
showTask(Stages[i].taskName[0], Stages[i].taskStatus[0], 1);
}
//...truncated
if ((Stages[i].taskName[5]!=null)&&(Stages[i].taskStatus[5]!=null)) {
showTask(Stages[i].taskName[5], Stages[i].taskStatus[5], 6);
}
}
}
}
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:285)
Its having a problem at line 46 with 'Debug.Log(Stages[i].taskName[0]);' particularly the Stages[i]
What am I doing wrong??
If you double click on the error, what line of code is triggering the error? Also can you past in the error message?
Answer by DaveA · Sep 21, 2013 at 08:02 AM
You are trying to initialize a lot of stuff outside of a Start function. And you have not allocated the arrays. For example:
function Start()
{
var Stage16 = new Stage();
Stage16.controlArray = new String[6]; // add this
Stage16.taskName = new String[6]; // add this
Stage16.controlArray[0] = "";
Stage16.taskName[0] = "findTheWaterCooler";
Updated code in OP.$$anonymous$$oved Stage initialization to start function and added the array initialization like you said. Still getting the same error.
yes same line. Its having issues with the list not the stages themselves as far as i can tell
Also this:
static var Stages : List.<Stage> = new List.<Stage>(22);
like
static var Stages : List.<Stage>;
Start()
{
Stages = new List.<Stage>(22);
\o/ not getting the arugement error anymore! Now im getting NullReferenceException: Object reference not set to an instance of an object tasks.OnGUI () (at Assets/Scripts/tasks.js:292) for same line :/