IndexOutOfRangeException at 0
I been getting the error "IndexOutOfRangeException: Array index out of range"
However I don't know why. The revelant code is:
// Variables
public Transform target; //Where I want the object to move
public Transform[] EndPoint; //List of possible positions, on the Inspector I set it to a size of 9 and fill every slot with an object
void Start() {
int i = Random.Range(0,9) ;
target = EndPoint [i] ;
}
Once I play I get the mentioned error. BUT I can see on the inspect that target has changed into one of the possible endpoints. If I ignore the error and resume the game it even works as intended and the object moves to position.
I have even tried removing the randomness out of the equation and instead using 'target = EndPoint[0]' but just as above, it still gives me an error.
While searching for an answer some people suggested resetting the script values in the inspector, which I did to no avail.
If the list of Transforms is filled in the Inspector before playing. Then this should be working without any errors. I'm thinking you accidentally placed the script on another GameObject and it's that empty list of Transforms that is throwing the error. In your project, right-click on the script and select "Find References In Scene". $$anonymous$$aybe you'll find something.
You're correct!
Checked all of my GameObjects and found the extra copy of the script. Now its working fine, thank you : D
Answer by bubzy · Nov 13, 2015 at 11:27 PM
try 0-8, like int i = Random.Range(0,8);
if you have an array of size 9 it will be number range 0-8 thus making 9 elements.
When dealing with int, the second value is exclusive. But I appreciate the help!