- Home /
IndexOutOfRangeExeption - Array index is out of range
Hello,
Does anyone know how to fix the error, I just can't figure it out and nobody else has encountered this error in combination with something like my script.
Here's the error: IndexOutOfRangeExeption: Array index is out of range. ScanConsole.Update () (at Assets/Scripts/ScanConsole.js:19)
And here's the script:
public var computer : GameObject;
public var textMesh : TextMesh;
public var cam : GameObject;
public var scanGui : GameObject;
public var index : int = 0;
public var inScanner : GameObject[] = new GameObject[1];
public var isExit : boolean = false;
public var isArrayDisplay : boolean = false;
function Start () {
textMesh = GetComponent(TextMesh);
}
//textMesh.text = "";
function Update () {
textMesh.text = "Selected: " +
inScanner[index].transform.name;
}
function OnMouseDown () {
if(isExit){
cam.SendMessage("ExitGUI",
SendMessageOptions.DontRequireReceiver);
}
}
Thanks!
$$anonymous$$ake index private and see if the error is still showing up. If not co$$anonymous$$g up again, it means you set index to be another value than 0 in the inspector. (And I did not downvote your quesytion)
Answer by ChrisJoosten · Apr 02, 2014 at 10:34 AM
You should initialize the list in the Start of Awake function
function Start(){
inScanner = new GameObject[1];
}
I'm used to C#, so not sure if this is right
He does that in the initialization. $$anonymous$$ore likely to be something playing with the index value.
Answer by varie-tea · Apr 02, 2014 at 12:36 PM
Making the index variable private worked, thanks a lot!
Your answer
Follow this Question
Related Questions
NullReferenceException array 2 Answers
Error CS0029 Help? (Screenshot of Exact Error) 1 Answer
array problem 1 Answer
Find index number in an Array of a transform by it's name 1 Answer
Texture2d[]: Array index is out of rang (Javascript) 3 Answers