- Home /
Array Index Out of Range
I'm having an array index out of range problem that I just can't seem to solve :S Does anyone have an idea? I've been looking it over and can't seem to locate the problem
currentWaypointLocation = -1;
var whichShell = ball.GetComponent("BallController").currentWaypointLocation;
ball.GetComponent("BallController").SetPosition(shells[whichShell].transform.localPosition.x, shells[whichShell].transform.localPosition.z);
ball.GetComponent("BallController").Show();
code addn
//Start with the middle one - base all other positions off the middle
shells[1].transform.localPosition.x = this.transform.localPosition.x;
shells[1].transform.localPosition.z = this.transform.localPosition.z;
shells[0].transform.localPosition.x = shells[1].transform.localPosition.x - .30;
shells[0].transform.localPosition.z = shells[1].transform.localPosition.z;
shells[2].transform.localPosition.x = shells[1].transform.localPosition.x + .30;
shells[2].transform.localPosition.z = shells[1].transform.localPosition.z;
//Starting 'numbers'
shells[0].GetComponent("ShellController").curPositionNum = 0;
shells[1].GetComponent("ShellController").curPositionNum = 1;
shells[2].GetComponent("ShellController").curPositionNum = 2;
//Set Reference Numbers -- For checking if user selects right shell later
shells[0].GetComponent("ShellController").shellReference = 0;
shells[1].GetComponent("ShellController").shellReference = 1;
shells[2].GetComponent("ShellController").shellReference = 2;
//Ball Stuff
ball.GetComponent("BallController").currentWaypointLocation = Random.Range(0,299) % 3; //Pick a position for the ball to start in
ball.transform.localPosition.x = shells[ ball.GetComponent("BallController").currentWaypointLocation ].transform.localPosition.x; //Set the position of the ball to whichever shell it is attached to
ball.transform.localPosition.y = shells[ ball.GetComponent("BallController").currentWaypointLocation ].transform.localPosition.y;
ball.transform.localPosition.z = shells[ ball.GetComponent("BallController").currentWaypointLocation ].transform.localPosition.z;
for( var ii=0; ii < shells.length; ii++ )
{
//If we're the first object
if( shells[ii].GetComponent("ShellController").curPositionNum == whichMove[0] )
{
//Set positions, and start moving
shells[ii].GetComponent("ShellController").localStartPosition = shells[ii].transform.localPosition;
shells[ii].GetComponent("ShellController").localEndPosition = shellPositionWaypoints[ whichMove[1] ];
shells[ii].GetComponent("ShellController").endPositionNum = whichMove[1];
shells[ii].GetComponent("ShellController").EnableMove();
}
if( shells[ii].GetComponent("ShellController").curPositionNum == whichMove[1] )
{
//Set positions, and start moving
shells[ii].GetComponent("ShellController").localStartPosition = shells[ii].transform.localPosition;
shells[ii].GetComponent("ShellController").localEndPosition = shellPositionWaypoints[ whichMove[0] ];
shells[ii].GetComponent("ShellController").endPositionNum = whichMove[0];
shells[ii].GetComponent("ShellController").EnableMove();
}
}
bAllReachedDestination = false; //No more setting coords, for now
}
//Stop moving shells when we hit the max number of moves
function StopWhenMovesDone() : boolean
{
if( curMoveNumber >= numMovesThisRound )
{
bRoundOver = true; //Round is over! NO MORE!
bRoundChoice = true;
//Make sure to put the Ball under the shell!
var whichShell = ball.GetComponent("BallController").currentWaypointLocation;
ball.GetComponent("BallController").SetPosition( shells[whichShell].transform.localPosition.x, shells[whichShell].transform.localPosition.z); //Set new position of whichever shell it's under!
ball.GetComponent("BallController").Show(); //Make the ball visible again!
Answer by Eric5h5 · Jul 05, 2011 at 08:18 PM
You're not actually showing any of the relevant code where this is used (also, use the code button to format code properly), but you can't use -1 as an array index.
Answer by SilverTabby · Jul 05, 2011 at 08:37 PM
Your error is that you are passing -1 into an array (currentWaypointLocation = -1)
Arrays can only accept int values in the range [0, arrayLength)
Also, you are calling GetComponent("String")
too often. GetComponent is a very slow method, and passing a string to it makes it even slower. If you are calling your function in a tight loop, you will see a major performance drop.
I have edited your code (below) to avoid calling GetComponent more than needed, and to catch the situation where you pass an illegal index. Also, it looks nicer like this.
currentWaypointLocation = 0;
var controller : BallController = ball.GetComponent.<BallController>();
var whichShell : int = controller.currentWaypointLocation;
if(whichShell < 0 || whichShell >= shells.GetLength() )
{
Debug.Log("ArrayIndexOutOfBounds! Setting to default value...");
whichShell = 0;
}
controller.SetPosition(shells[whichShell].transform.localPosition.x, shells[whichShell].transform.localPosition.z);
controller.Show();
Your answer
Follow this Question
Related Questions
Arrays rebels to my power! 1 Answer
Array Index out of range? 1 Answer
Array index is out of Range!? 1 Answer
Index out of range exception - C# - Can't find the problem 1 Answer
What means this error ? 1 Answer