I am doing a Unity tutorial and have run into an error. OutofRangeException : Array index is out of range. What does this mean and how do i fix it? Here is my code :
#pragma strict
//Enemy Script
//Inspector Variables
var shapeColor : Color[];
var numberOfClicks : int = 2;
var respawnWaitTime : float = 2.0;
//Private Variables
//Update is called every frame
function Update ()
{
if (numberOfClicks <= 0)
{
var position = Vector3 (Random.Range(-6,6),Random.Range(-4,4),0);//creates random position
RespawnWaitTime();
transform.position = position; //moves game object to a new position
numberOfClicks = 2;
}
}
//RespawnWaitTime is used to hide game object for a set amount of time and then unhide it.
function RespawnWaitTime ()
{
renderer.enabled = false;
RandomColor();
yield WaitForSeconds (respawnWaitTime);
renderer.enabled = true;
}
//RandomColor used to changematerial of a game object.
function RandomColor ()
{
var newColor = Random.Range(0, shapeColor.length);
renderer.material.color = shapeColor[newColor];
}
Thank you so much it is working fine now!
– JonWagIf you question is answered, click on the checkmark next to the best answer to close it out. Thanks.
– robertbuHi robertbu! :) This answer is really helpful!! I had the same error. I was searching all over my script for almost an hour trying to find out what's wrong. Until I saw this post then tried to look at the inspector for my objects. I found out that what's wrong. I used the incorrect Tag for my objects. Thanks a lot!! I thought I would have scratch my script. :)
– GrafSpee