- Home /
Material Instances Problem.
I'm having a problem with a GameObject that contains 2 materials. I'm doing the following:
renderer.sharedMaterials[ 1 ] = Resources.Load( materialPath ) as Material;
renderer.materials[ 0 ].mainTexture = Resources.Load( texturePath ) as Texture;
So i'm trying to change the 2nd material in all GameObjects but keeping it batched. This works until i change the 1st material's mainTexture to a different one in each GameObject, after doing this the 2nd material becomes an Instance.
I would like to know if its possible to have one of the materials batch and the other be an instance. Thank's in advance.
If you store a $$anonymous$$aterial in a var, then set this material(var) to multiple objects at run time, then they will batch as if you had manually set the texture to them all in the editor. This may still give you one extra DC but maybe it could help you...
Answer by robertbu · Oct 17, 2014 at 11:13 PM
If you change any property of a material at runtime...and that includes changing any material in the materials array...you get a new material instance. The typical solution to this problem is to use a texture atlas. That way you can have multiple textures combined in to a single texture and therefore use a single material to present multiple different (and differently textured) objects.
Well but my problem is that one of the 2 materials in the array is the same in all objects and the other is different in all objects. But both of them are marked as instances. Sorry if you answered this already but i didn't understand fully.
Your answer
![](https://koobas.hobune.stream/wayback/20220613165235im_/https://answers.unity.com/themes/thub/images/avi.jpg)