- Home /
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..
Have you got the array to work? Can you access and populate it via the inspector panel?
still nothing... I have already populated it with 2 different prefabs in the inspector panel
Answer by ErickP · Nov 11, 2012 at 08:58 PM
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!
Answer by Kmulla · Nov 10, 2012 at 04:36 PM
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.
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.
I have taken the initialization line out, but now I am getting an "array out of range" error.. Same thing happens when I increase the size of the array in the inspector
If I instantiate civilianPrefabs[0] it strangely creates the object in the Scene but generates the "array index is out of range" error, stopping the execution afterwards
If I recall correctly, the "array out of range" error occurs when you try to acces an element in the array with a higher index than the length of the array. If it's the Debug.Log(civilianPrefabs[0]); that gives the error, you might have forgotten to populate the first object in the array. If that's not the case, will you please post your code then? :)
In this case I am not populating the array with the script... It should already have 2 elements (like you have shown in the picture above). for some reason the script is not recognizing civilianPrefabs[0] even when I should have 2 elements in this array . the current code is: var civilianPrefabs : GameObject[]; function Start () { Debug.Log(civilianPrefabs[0]); }
I've tested the code in my own project and it works just fine. Is your gameObjects, in the array, destroyed? Otherwise I'm not sure I'm able to find the error.
I bet your project contains other scripts. Are you accessing the array through any of them?