Inspector Array Size Overriding Start() Initialization

Hello,

I’m really not sure what’s going on here. It seems that the inspector just ignores what my code does. I had the following in my code.

public var gameObjectArray : GameObject[];

function Start() {
    gameObjectArray = new GameObject[4]; 
}

But when I would attempt to access gameObjectArray later, I came to discover that the array length had been reset to 0 as soon as the Start function ended. (I printed length at the end of start and the first next moment that the code came across this array I printed length and found it was 0. Before anything was done with the array.)

I overcame this obstacle by setting the size directly in the inspector. However, this can’t work for another array I need which is 2-dimensional. Essentially,

public var someIntMatrix : new int [,];

function Start() {
    ***random level generation***
    ***someIntMatrix values set based on that random level generation***
}

I want to make sure that when start ends for this, I don’t lose all of those values.

My two questions are:

In the first code example, why am I losing the coded size of the array at the end of Start?

In the second code example, will I lose the someIntMatrix value just as before? How do I avoid this?

My understanding of the inspector is that it isn’t the last step in variable assignment and that in the first example my code should be the last deciding factor of setting the size of the array. However, that wasn’t the case unless I was doing something wrong.

Thank you for your help.

EDIT

It turns out I misdiagnosed what was happening (nor was I specific enough in this question to get the help I needed). So what was happening is I was instantiating some Prefabs which contained the array. I then immediately accessed those arrays in the prefabs. But, the array wasn’t itself initialized until the Start() call, which I didn’t realize didn’t happen as soon as Instantiate was called but waited to happen until that entire random level generation code block finished. Moved the code from Start to Awake and all is dandy now.

Put this line before your declaration:

@System.NonSerialized
public var gameObjectArray : GameObject[];

The variable will still be public, but wont be seen in the inspector (preventing any forced overwrite).