I’m creating a number of GameObjects at runtime using:
new GameObject ("NewObject");
and then creating its mesh (vertices, triangles, …) and components in script. All this works fine, but I’m trying to figure out how to assign one shared material to all these objects. When changing for example the texture of this material, I want it to change in all the objects using this material (and hopefully save some drawcalls in the process).
I tried using:
object.renderer.sharedMaterial = material;
But that still seems to create a difference instance for each object?
Any help would be appreciated.
I found the reason of my problem. After assigning a sharedMaterial I was doing
if (object.renderer.material.mainTexture.width > ...)
Apparently just referring to mainTexture is enough to “break” the sharedMaterial and create an instanced material instead.
So I changed it to:
if (object.renderer.sharedMaterial.mainTexture.width > ...)
Now changing the material’s texture changes the appearance of all objects, as intended. However I did not get any saved drawcalls, but I’ll look into that later.
Try to asign the rendere.material instead of sharedMaterial.
The ‘material’ variable is the material instance used during runtime.