0

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?

  • Manipulating one material instance does not affect other material instances, whether you use images or sprite renderers, so your described approach should work if done correctly. Have you made sure (e.g. by checking the material GUIDs in the debug inspector), that you did not by accident assign the same material instance to all images? Also, since you talked about prefabs - if you change the material of a prefab at edit time, and there are no override materials in e.g. the scene, then of course all your prefab instances will again refer to the same material. – michi-b Apr 22 '23 at 17:12
  • Hey thanks for your response. I've tried the exact same code with a SpriteRenderer and it works in terms of changing the colours individually. It seems impossible to create an (Instance) of an Image's material, it always makes a (Clone). I literally replace the Image for Sprite Renderer with no code change and it makes an Instance not a Clone... so weird. I can't use a sprite renderer though as it doesn't work well in UI/Canvas... Another weird thing is while running the game in the editor I can change the Image's Material's Colour and it changes only this objects/materials... – Anthony Brown Apr 22 '23 at 17:32
  • I found the solution was that when Instantiating my prefab with the Image on it, the Awake method inside the MaterialInstancer class that was attached wasn't being called before the further .material.setColor() (after instantiating) was called. Not what I would expect, but I just made a seperate method in the MaterialInstancer called .SetPrimaryColor() which sets the colour. This runs after the awake has instantiated the material so it works... weird. – Anthony Brown Apr 22 '23 at 20:39

0 Answers0