How to access gameobject array set in inspector?

I´m having trouble with a somewhat simple task but could not find exactly what is happening in the forum…

The idea is to create an array of gameObjects which will be populated via Inspector tab.

The code below is returning null for the civilianPrefabs[0] Debug call, even though I have set the GameObject array with size 1 and with a prefab as the first element.

var civilianPrefabs : GameObject[];

function Start () {
  civilianPrefabs = new GameObject[2];
  Debug.Log(civilianPrefabs[0]);
}

Another question: is it really necessary to instantiate the gameObject array with its size? As it has already been populated by the inspector I don´t see the need for that…

In the inspector tab you can change the size of the array by changing the number 1 to the number of GameObjects you want in the array.

[4848-skærmbillede+2012-11-10+kl.+17.29.07.png|4848]

As for your code. In the first line of the function Start() you overwrite the Gameobject array with a new one, which contains two empty GameObjects, which is why you get a null reference.

You can se it yourself by moving the Debug log to the first line of Start(), where it’ll return your GameObejct.

The issue at hand was a Unity bug: the initial script had a GameObject array populated with the inspector tab. When trying to access any index number Unity would return an “index out of range” error.

The issue was solved by creating a new script with exactly the same code and replacing it in the gameobject.

Thanks everyone for the answers!