- Home /
Array index is out of range
Hello, I am new to arrays. I want to have an array containing 3 float values, which I am able to declare. The console gives error for the last line: "Array index is out of range".
using UnityEngine; using System.Collections;
public class ChangeArray : MonoBehaviour {
//public string[] myArray = new string[2];
public float[] myArray2 = new float[2];
// Use this for initialization
void Start () {
// myArray[0] = "first value";
// myArray[1] = "second value";
// myArray[2] = "third value";
myArray2[0] = 5f;
myArray2[1] = 10f;
myArray2[2] = 15f;
}
}
Answer by robertbu · Oct 23, 2014 at 06:05 AM
The size of your array is 2, but you are putting three values in it. This line:
public float[] myArray2 = new float[2];
...creates an array with 2 slots (0 and 1). Note that since the array is public, changing the 2 to 3 will not fix the problem. You need to either make the change in the Inspector, change the size and in the Inspector use the Reset for this object, or change the value and make the array private. I vote for the last if it doesn't interfere with something you have planned.
Robertbu, thanks, I tried changing 2 to 3 in inspector and error disappered! But why did it happen? Should I always change it via inspector, when creating an array? :D
With a public class instance variable, the initialization happens at the time the script is attached to the object. Changes to the initialization code after the script is attached are ignored. If you look on the right hand side of any component on a game object in the Inspector, you will see a gear icon. Clicking on the gear icon will show a dropdown that includes a 'Reset' option. Selecting this option will cause the component to reinitialize the component. So you could have made the change in script and then use 'Rest'.
But unless you have a reason for making it public, the best solution is to just make it private:
private float[] myArray2 = new float[3];
Any changes you make to initialization of private variables will happen when the script is recompiled.
It happened because you were trying to put 3 people in a 2 person car, as an analogy. You have to create the array with however many objects you want to store in it, so you're storing 3 items in your array it has to be [3] if you were storing 100 items in the array it would have to be [100] so on
Answer by Fewpwew130 · Oct 23, 2014 at 06:31 PM
Thank you guys for help!
Robertbu, I named it public, because I was not quite sure what the difference and limitation of private was, apart from being hidden from the inspector. I thought private variables could only be accessed inside a single script and therefore could not be got by using .GetComponent script, to get the value for another script. :D
Your answer

Follow this Question
Related Questions
Array index out of range after build 1 Answer
Array out of index? 1 Answer
How do I check a List[0], when it's nothing? 1 Answer
How to check if inspector-filled array is empty 0 Answers
Out of range error 1 Answer