Array index out of range for 39 in 399

Hey all, the compiler tells me that my array is out of range when it is clearly not. Here are the important parts of the code:

public var cubeInstance 	= new GameObject[16];
public var nodeInstance		= new GameObject[400];
var nodeIsInstantiated		= new boolean[400];
var cubeIsInstantiated 		= new boolean[16];

....

function CalculateNodes() {
	for (var a : int = 0; a < 16; a++) {

....
	
	if (cubeIsInstantiated[a] && a > 11 && !nodeIsInstantiated[a+24]) { //top, 4-7
		nodeIsInstantiated[a+24] = true;
-->     nodeInstance[a+24] = Instantiate(Resources.Load("Node_prefab"),     cubeInstance[a].transform.position + new Vector3(0,.5,0), new UnityEngine.Quaternion());
	}
	
	else if (!cubeIsInstantiated[a] && a > 11 && nodeIsInstantiated[a+24]) {
-->    	Destroy(nodeInstance[a+24]);
		nodeIsInstantiated[a+24] = false;
	}

....
}

… where code is omitted
→ where the compiler gives me the error

The compiler error:
IndexOutOfRangeException: Array index is out of range.
(wrapper stelemref) object:stelemref (object,intptr,object)
Main.CalculateNodes () (at Assets/Scripts/Main.js:131)
Main.OnGUI () (at Assets/Scripts/Main.js:92)
UnityEditor.Toolbar:OnGUI()

(same for both lines)

Note also that I successfully try to call a variable with the exact same array size on the lines next to the ones that the compiler rejects.

Pls halp!

If Unity tells you it’s out of range, then it’s out of range. There’s no other interpretation of that. I’d recommend not initializing array sizes like that, since public variables take their values from the inspector, not your code. So just because you have “new GameObject[400]” doesn’t mean there are 400 entries in the array; it depends on what you have in the inspector. Instead, initialize the array size in code properly:

var nodeInstance : GameObject[];

function Start() {
    nodeInstance = new GameObject[400];
}

Just FYI, variables are public by default, so “public var” and “var” are the same…if you’re going to specify it explicitly, best to be consistent. If you want a variable to be private, you have to specify “private”.

Follow the advice here.

http://forum.unity3d.com/threads/46032-Array-of-GameObjects-solved

This is fixed now, it was actually a combination of about 4 problems. Thanks for all the good advice, I needed it!