"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 *);*

_ anglesUnityArray.Add( anglesToPlanets );// (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 > highestSoFar){ // Update highest
highestSoFar = anglesBuiltinArray*;
indexOfHighest = i;
}
}
// Add to Ordered List and Remove it from both arrays*

//planetsInRouteOrder.Add( planetsBuiltinArray );
* Debug.Log("Added so Far: "+addedSoFar.ToString());
planetsInRouteOrder[addedSoFar] = planetsBuiltinArray; // 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);*
* }*

The exception occurs due to the planetsBuiltinArray* 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] ?*

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*; // 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);