- Home /
"Array index is out of range" for any index value, when array.length>index
Like the title says, I always get an "array index out of range" error, even though the index I'm using is zero (for example) and the array has a length of 4 (or whatever). I'm utterly confounded. This is too simple to not work...
Here is the Code: (error line about 3/4 of the way down)
var planetsInRouteOrder : GameObject[]; // This oughta' hold 'em.
planetsInRouteOrder = new GameObject[numPlanets]; // numPlanets will be, for example, 4
Debug.Log(planetsInRouteOrder.Length); // This checks out just fine.
var planetsUnityArray = new Array(); // This will hold the ones we're checking
var anglesUnityArray = new Array(); // Same deal ^ but for angles
var planetsBuiltinArray : GameObject[]; // Builtin versions to let me reference their values...
var anglesBuiltinArray : float[];
for (i=0; i<numPlanets; i+=1) { // Add just the planets we're using this game, to the Arrays
planetsUnityArray.Add( listOfPlanets[i] );
anglesUnityArray.Add( anglesToPlanets[i] );// (and add their angles)
}
var highestSoFar : float;
var indexOfHighest : int;
var addedSoFar : int = 0;
while (planetsUnityArray.length > 0){ // Until the Array is Empty
planetsBuiltinArray = planetsUnityArray.ToBuiltin(GameObject); // Convert to Builtin so we can Reference their Values
anglesBuiltinArray = anglesUnityArray.ToBuiltin(float);
highestSoFar = -1; // Reset these
indexOfHighest = -1;
for (i=0; i<planetsUnityArray.length; i+=1) { // Find Highest Angle Still In Array
if (anglesBuiltinArray[i] > highestSoFar){ // Update highest
highestSoFar = anglesBuiltinArray[i];
indexOfHighest = i;
}
}
// Add to Ordered List and Remove it from both arrays
//planetsInRouteOrder.Add( planetsBuiltinArray[i] );
Debug.Log("Added so Far: "+addedSoFar.ToString());
planetsInRouteOrder[addedSoFar] = planetsBuiltinArray[i]; // ERROR IS HERE - Even a hard coded index causes an out of range error. But using the current index always fails at zero.
addedSoFar +=1;
planetsUnityArray = new Array(planetsBuiltinArray); // Convert Back so we can Remove.
anglesUnityArray = new Array(anglesBuiltinArray);
planetsUnityArray.RemoveAt(i);
anglesUnityArray.RemoveAt(i);
}
Answer by simonmc · May 31, 2012 at 03:56 AM
The exception occurs due to the planetsBuiltinArray[i]
section of the line in question. the i var is set to the length of the array arfter the previous for loop. Perhaps you meant to do planetsBuiltinArray[indexOfHighest]
?
I'm still getting a similar error, with different wording. Now it says the index is -1 or too high (the variable I put in is indexOfHighest). I'm guessing maybe it isn't being set at all. Or something. Too tired. Look at it tomorrow.
Answer by whydoidoit · May 31, 2012 at 03:59 AM
Surely these lines should be using indexOfHighest not i? i will always be beyond the array range after the for next loop...
planetsInRouteOrder[addedSoFar] = planetsBuiltinArray[i]; // ERROR IS HERE - Even a hard coded index causes an out of range error. But using the current index always fails at zero.
addedSoFar +=1;
planetsUnityArray = new Array(planetsBuiltinArray); // Convert Back so we can Remove.
anglesUnityArray = new Array(anglesBuiltinArray);