- Home /
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 :P
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
Answer by robertbu · Dec 30, 2013 at 09:19 AM
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?
Sir, you are a hero!!!! Thank you very much! Now I can finally go to sleep xD
Your answer
Follow this Question
Related Questions
Resizeable gameObject array. 1 Answer
Array of Arrays of GameObjects/Prefabs (C#) 1 Answer
Array problem -3 Answers
Instantiating from an object that's in an array 1 Answer
How can I instantiate a single prefab from an array? 0 Answers