Array of prefabs creation question.

What is the problem with the following way to create an Array of prefabs?

I want to instantiate randomly selected prefabs contained in this Array at certain positions.

So I began creating the js that will do this. Till now, the js contains only the following lines:

var spawnObjects : GameObject[];
var spawnObject0: GameObject;    
var spawnObject1: GameObject;
var spawnObject2: GameObject;
var spawnObject3: GameObject;
var spawnObject4: GameObject;
var spawnObject5: GameObject;
var spawnObject6: GameObject;

function Awake () {
spawnObjects[0] = spawnObject1;    
spawnObjects[1] = spawnObject1;
spawnObjects[2] = spawnObject2;
spawnObjects[3] = spawnObject3;
spawnObjects[4] = spawnObject4;
spawnObjects[5] = spawnObject5;
spawnObjects[6] = spawnObject6;

At the Inspector (while the object the js above is attached to), I have filled the slots that appear, with the desired prefabs.

When I hit play, I get the following warning:

IndexOutOfRangeExeption: Array index is out of range.

The console points at line 10 of the js (spawnObjects1 = spawnObject1;).

This is how the Inspector looks like when I select the object that has this js attached (I cannot understand why the Size of the array is 0):

alt text

As well as declaring the array, you need to also initialize it, so you need to use something like:

var spawnObjects : GameObject[] = new GameObject[7];
// (or do this in the Awake function instead)

(Eric's answer is most helpful in this case, since you're setting the values from the editor anyway, but I've certainly found it useful to set up arrays in a script before. That way I have a list of values that I can copy paste somewhere else if need be, instead of having to fiddle around in the editor if I want to move or even just rename the array (a pretty huge downside to using the editor for this stuff)).

Remove all that, and just use

var spawnObjects : GameObject[];

The point of an array is that you don't need to use separate variables. Arrays are numbered starting from 0, not 1.