- Home /
How to change material without creating a new material instance?
How to change material without creating a new material instance?
Using renderer.material = myOtherMaterial;
will create a new instance of myOtherMaterial
I need it to be the same instance so GPU instancing will work.
Answer by Bunny83 · Apr 29, 2020 at 12:23 AM
You have to use sharedMaterial / sharedMaterials instead of material / materials.
I see, but myOther$$anonymous$$aterial is a variable so how would I do that? Calling renderer.shared$$anonymous$$aterial = myOther$$anonymous$$aterial;
will still cause a new instance of the material to be created
No, it won't. When you assign a material instance to shared$$anonymous$$aterial Unity will just use that material. Note that you must not touch material or materials even once. This is also true for reading. As soon as you read or write anything from / to the material property Unity will instantiate the currently assigned material for this renderer.
Oh thanks, I didn't notice that I was reading the material at a later point
I am facing the same issue, and as far as I can see .material and .shared material call the same function inside the setter in the Renderer class. The only solution I have found is to treat it as setting a material on a renderer will always create a new instance. Perhaps this is a bug?
I am also facing the same problem, and as far as I looked into the Renderer.CS file, both Renderer.material and Renderer.sharedMateral set function calls the same internal function. WIthout changing any material properties, it is still creating a new material instance for me. I suggest you test this out as well.
Your answer
Follow this Question
Related Questions
Material doesn't have a color property '_Color' 4 Answers
How can I set the material on an instance after creating it? 1 Answer
iTween vs GoKit 1 Answer
how do i create material instances without errors 4 Answers
Using Same FBX to load multiple, but want different Color Settings on each, how to do this? 1 Answer