Load Prefabs in Array with Javascript

During the past few hours, I was trying to load prefabs into an array -.- Maybe I need some help :stuck_out_tongue:

When I use a normal variable like…

private var temp : GameObject;
temp = Resources.Load("MyPrefab");

…everything works fine. But when I try to load the same prefab into an array like this…

private var temp : GameObject[];
temp[0] = Resources.Load("MyPrefab");

…I get the following error:

NullReferenceException: Object reference not set to an instance of an object

You haven’t created any space for the game object in the array. That is ‘temp’ is a pointer that has not be initialized. If you know the number of entries that you will be filling, then you can create space this way:

private var temp : GameObject[] = new GameObject[5];

If you don’t know the number of entries you will need, then you can use a .NET generic List.

For more info on Arrays and Collections (including generic Lists) see:

http://wiki.unity3d.com/index.php?title=Which_Kind_Of_Array_Or_Collection_Should_I_Use?