- Home /
NullException Error With Array of GameObjects
I am trying to create an array of gameObjects, which is then referenced by a function that turns on the renderer and collider of each when called. However, when I playtest, occasionally it will work for one of the cylinders, and the rest of the time it throws a NullReferenceException. I was wondering if anyone can see what I did wrong? I've looked through other similar questions and still cannot figure out why it is doing this.
Here is the instantiation (for reference, there are four cylinders/rings in the scene).
private var Rings : GameObject[] = new GameObject[10]; //array of rings
function Start () {
//count rings
totalAmount++; //totalamount = totalamount +1;
Debug.Log("Total Amount of Rings = " + totalAmount);
Rings[totalAmount] = GameObject.Find("Cylinder." + totalAmount);
Debug.Log("Found game object: " + totalAmount);
}
And here is the function where it is called.
function TurnOnAllRenderers(){ //turn all ring renderers back on if the player is reset to beginning
for(var i = 1; i <= totalAmount; i++)
{
Debug.Log("turn on = " + i);
Rings[i].renderer.enabled = true;
Rings[i].collider.enabled = true;
}
}
Answer by fafase · Nov 23, 2013 at 07:42 AM
Your array only stores one item in the Start at the index 1 so your loop in the method does not find anything.
EDIT: based on your comment, the error is different. So I would guess totalAmount is static since it increases as expected. But your array is private You actually have an array for each script.
So the first script1 places object1 in slot 1 of array1 using totalAmount, but script2 places object2 in slot 2 array2 using totalAmount.
Solutions:
Easy: make the array static.
More complex: Store the array in some Manager script and use GetComponent and find the array:
var manager :Manager;
function Start(){
manager = GameObject.Find("Manager").GetComponent(Manager);
manager.array[totalAmount] = object;
}
Also, try to start from 0 as you may get out of bound.
There are multiple instances of the script, so totalAmount rises to four by the end of the instantiation of the objects (since each has the script). The debugs in the first return:
Total Amount of Rings = 1 Found game object: 1
Total Amount of Rings = 2 Found game object: 2
Total Amount of Rings = 3 Found game object: 3
Total Amount of Rings = 4 Found game object: 4
Answer by V4mpy · Nov 23, 2013 at 09:03 AM
Use update for increasing the total amount - or use a loop to do this. Also start a loop at index 0.
Your totalAmount is 1. So your loop goes from 1 to 1. -> the loop is doing nothing
There are multiple instances of the script, so totalAmount rises to four by the end of the instantiation of the objects (since each has the script). The debugs in the first return:
Total Amount of Rings = 1 Found game object: 1
Total Amount of Rings = 2 Found game object: 2
Total Amount of Rings = 3 Found game object: 3
Total Amount of Rings = 4 Found game object: 4
Your answer
Follow this Question
Related Questions
I want to add cloned gameobjects that are triggered, to an array. 2 Answers
NullReferenceException with Javascript Array 2 Answers
Array of custom properties? (C#) 1 Answer
How to get values from JSON and makes array of GameObject 1 Answer
Draw a line from gameobject at end of the array to the gameobject in the start of the array 1 Answer