I'm generating an array of objects using this code:
while(currentPosition.z < poolSize){
var ringObj = GameObject.Instantiate(ring) as GameObject;
ringObj.transform.position = currentPosition;
ringObj.GetComponent<Renderer>().material.color = new Color (1.0f, 0f, 0f);
// Adds the ring to the pool.
pool[(int)currentPosition.z] = ringObj;
updateCurrentPosition ();
}
This works fine without this line:
ringObj.GetComponent<Renderer>().material.color = new Color(Random.Range(0.0f,1.0f), Random.Range(0.0f,1.0f), Random.Range(0.0f,1.0f));
Which should randomize the color of the object. The problem is that instead of doing this, the whole code would generate only one object (I can see it on the hierarchy view) with the standard material color and then stops the generation. Also, the material is not changing color.
I can't understand why this happens. Can someone explain this to me, please? I'm using Unity 5.3.4f1. Thank you.