Unable to change Instantiated Object's Color

I’m working on a game in which I instantiate objects. Then, I use ray casting to highlight an object that was previously instantiated (it changes the color of that object (GetComponenent().material.color = Color.red).

I then have a clumsy mechanism attached to the instantiated objects that changes the color back to the original color. Roughly it does:

Color originalColor;

void Start(){
  originalColor = gameObject.GetComponent<Renderer>().material.color;
}

void Update(){
  gameObject.GetComponent<Renderer>().material.color = originalColor;
}

This clumsy code works fine when on a non-instantiated object - when I raycast against it, it turns red, when I raycast away, it turns back to the original color. But if I instantate an object (that has this script attached to it), it will highlight red, but not turn back to its original color.

I do notice that when I instantiate an object at runtime, the material itself seems to be instantiated too (MaterialName (Instance)(Instance) is how it shows up in the property manager). Any ideas why this wouldn’be working?

Thanks.

I’m getting this bug too

Can you try to put the originalColor bit from Start() and into Awake()?? I think you are running into a race condition :smile: