Reputation: 1
Title says it all, I've looked online for multiple solutions. I'm instantiating a Prefab multiple times for my UI, I'm trying to set the child of this prefabs Image components Materials Colour (what a chain of words), and its resulting in them all being the same colour as the last one instantiated (I believe the entire material colour is being changed).
I originally tried
hair.material.SetColor("_HairColour", hairColour);
and was expecting the individually instantiated prefab to change color, not every prefab using this material.
I have almost the exact same code working fine for my actual game objects using SpriteRenderers. It seems to be something with the Image component..
I've tried forcably making Clones of the material from the material & the shader like so, it does indeed make (Clone)'s but it still acts the same way in both cases...
GetComponent<Image>().material = Instantiate(GetComponent<Image>().material);
Material instance = new Material(shader); instance.CopyPropertiesFromMaterial(material); GetComponent<Image>().material = Instantiate(instance);
I've even added (temporary) public Color to the objects in a script and assigned it to the same colour as what should be getting set in the material and each object has its own different colour, just the material is always the last colour...
I'm at a loss here, so any assistance would be appreciated!
Edit: I've updated the Image components to be SpriteRenderers and it works perfectly, but I was told not to use SpriteRenderer in UI so this seems backwards. Image component doesn't work with materials?
Upvotes: 0
Views: 1259