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*.renderer.enabled = true;*
_ Rings*.collider.enabled = true;_
_ }_
_}*_