- Home /
Why changing color on renderer.materials creates new instance of that material
Hi,
I have a GameObject with a Mesh Renderer. Within the Mesh Renderer, there is a materials array of size 1000. However, they all are pointing at the same material. I would like to modify the material on runtime. So I looped through the array to change the colors. Then while the game is running, i looked at the inspector of that gameobject; there are a thousand of that material! How do I avoid that? I tried sharedMaterials, but the changes persist after the game is stopped... I don't want that to happen as well.
What option do I have now?
Thanks!
Answer by DaveA · Aug 23, 2012 at 10:04 PM
You could make a copy of the material in a Start routine, set that as the shared material, then go about making your mods. Thus you are modifying, as a sharedmaterial, the copy, not the original (disk) version.
A material is a shader which uses color(s) and texture(s). So many materials may share a color, and many materials may share a texture. But any change in any of that means it's not the same material anymore, so it will make a copy, then set the value you changed on that copy.
Thanks for the response. But I think I tried doing something like shared$$anonymous$$aterials[2] = custom$$anonymous$$aterial; and it didn't work