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.

  • \$\begingroup\$ Thank you for the clarification, I didn't notice that. I've edited the question. \$\endgroup\$
    – Aurasphere
    Apr 3, 2016 at 14:58
  • 1
    \$\begingroup\$ Can you please show what is in updateCurrentPosition ? \$\endgroup\$
    – lvictorino
    Apr 3, 2016 at 19:23
  • \$\begingroup\$ I've added the code you asked for, but as I said, it works flawlessy without that specific line of code. Thank you. \$\endgroup\$
    – Aurasphere
    Apr 3, 2016 at 19:34

1 Answer 1


Place this on an empty GameObject in the scene:

public class TestColourAndPosition : MonoBehaviour
    void Start ()
        for (int i = 0; i < 10; i++)
            GameObject cube = GameObject.CreatePrimitive(PrimitiveType.Cube);   
            cube.transform.position = new Vector3(i * 2.0f, 0, 0);
            cube.GetComponent<Renderer>().material.color =  new Color(Random.Range(0.0f,1.0f),Random.Range(0.0f,1.0f),Random.Range(0.0f,1.0f));

    void Update ()

...No problem with generating random material colours or simple GameObject positioning.

So I suspect your problem comes before that, and is corrupting some values leading to the crash.

I suggest you use the above and work slowly and steadily back toward what you want. And don't call updateCurrentPosition() (in Update() or LateUpdate()) until you can position the objects statically / once off, without experiencing any problems. And switch in your 3D model only once all else is working, in case there is a model problem - unlikely, but possible.

Morals of the story When stumped, revert to a minimal test case and make your way from there. Always test incrementally as you code - so you know the exact change where things begin to fail.

  • \$\begingroup\$ Thank you for your answer. I tryed your script and I've figured out that the problem is caused by me instantiating the model. In fact it works fine with both your example and in mine using the PrimitiveType.Cube instead of my model. But I have still no clue on what's happening... \$\endgroup\$
    – Aurasphere
    Apr 3, 2016 at 21:01
  • \$\begingroup\$ @Aurasphere Hmm... I half suspected that. Sometimes I have seen meshes give strange problems in Unity if they are not built correctly. If you can recreate the model in e.g. Blender, maybe try doing that. Or import it into your 3D editing tool and see if there are any obvious problems...? \$\endgroup\$
    – Engineer
    Apr 3, 2016 at 21:35
  • \$\begingroup\$ Yeah the model was the problem and I was able to fix that, although the color is still not changing. It works fine if I use primitive types though... \$\endgroup\$
    – Aurasphere
    Apr 9, 2016 at 15:18
  • \$\begingroup\$ @Aurasphere Do you know what colours you want to use at runtime? If so, and if they are few, set up several materials in the Editor, set their colours, and just use them by name at runtime, rather than setting .color directly.. \$\endgroup\$
    – Engineer
    Apr 9, 2016 at 15:23
  • \$\begingroup\$ I've tried it but it's still not working. If I click on the instantiated object in the hierarchy I see the material with the correct color I associated at runtime (so the program part is working) but the game view shows the prefab color. If I change the prefab color, the game view changes, but programmatically I've no way to change it. I have this exactly problem with the old solution. The only thing I could think of is that I'm missing a call to a method that refresh the renderer... Could it be? Thank you. \$\endgroup\$
    – Aurasphere
    Apr 10, 2016 at 11:36

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .