Array Resizing

I am trying to set up a built in array and continue to get the error:

The built in array itself works fine and I can change the size in the inspector with no problems. I can even reference the individual data fine via script at this point. The problem comes when I try to resize the array in script. The array length shows the proper size and the fields update in the inspector after the resize, but every slot I try to access in code returns the null reference error above.

Here’s the code:

class myClass
{
	var variable1 : int = 1;
	var variable2 : int = 2;
	var variable3 : int = 3;
}
var myClassArray : myClass[];

function Start () 
{
        //myClassArray is set to 5 in the inspector manually
        Debug.Log(myClassArray.length); //This returns 5 as expected
        Debug.Log(myClassArray[0].variable1); //This returns 0
	myClassArray = new myClass[200]; //This works fine and updates the fields in the inspector as expected
	Debug.Log(myClassArray.length);  //This returns 200
	Debug.Log(myClassArray[0].variable1); //Should return 1, but this throws the error above even though the inspector shows the correct data
}

Any suggestions as to what’s going wrong would be greatly appreciated. Thanks.

When you do “myClassArray = new myClass[200];”, all the entries are set to null. The code is running correctly (myClassArray[0].variable1 definitely should not return 1, and should throw an error until initialized), but the inspector isn’t showing the correct contents of the array; I’d consider that a bug.

–Eric

Thanks, that’s what I was missing. I was assuming that the array was populated with the class data automatically once I resized it since the inspector was showing it. I threw in this line immediately after the resize and everything works:

myClassArray[0] = new myClass();