- Home /
Material Versus Shared Material
I am working with the renderer for the very first time and I wanted to make sure that I understood exactly how these work and the difference between the two.
From what I understand and can see from tests, renderer.sharedMaterial returns the actual instance(s) of the Material at renderer.sharedMaterials[0] that is on the object. This can be modified, and it will affect every use of this Material in the Scene.
renderer.material, on the other hand, creates a copy of renderer.sharedMaterials[0], which then replaces the original in renderer.sharedMaterials[0], and returns this new material. A duplicate object from the scene will not be affected to changes made to this material, and changes made by said duplicate object to it's respective renderer.sharedMaterial[0] will not affect this new copy.
render.sharedMaterials returns the actual array (well a copy of the array(not the materials just the array)) of the materials. This particular array only applies to that particular renderer. Though the materials are shared, the arrays are not. Removing or adding a material to this array will not change any of the other objects in the scene. Changes to the materials them selves, however, will.
render.materials copies each Materials from renderer.sharedMaterials, replaces the originals with the copies in the array, then return a copy of the new array back. Again any of these materials can be manipulated without affecting or being affected by changes to the original material.
I hope that was not too long.
Do I have all of this correct, is there anything I am missing? I know this is not much of a question, per say, but I was left unsure from the Unity documentation. I apologize if I have taken an unfair amount of your time.
Actually this question proves how poorly designed some APIs are.
Answer by rutter · Mar 19, 2012 at 01:20 AM
From what I know, your understanding sounds right.
You may want to stress test these assumptions before using them in production, though. Among other things, I don't know if copying all of those materials may be a performance problem.
Answer by Pratap-Dafedar · Feb 20, 2014 at 02:48 PM
It is because suppose someone want to change the material colour or texture, then it should not effect original material. So renderer.material will be used. And If you want to do so, then use renderer.sharedmaterial. So, It is flexibility from unity API, not a weakness.