- Home /
Can't set sharedmaterials
So recently I've been trying to add some basic optimization to my code by using using a shared material. I've got my code working originally with a non shared one by using the line:
tempFace.GetComponent(MeshRenderer).material.SetTexture("_MainTex", Resources.Load(type, Texture2D));
I've tried to change this to:
tempFace.GetComponent(MeshRenderer).sharedMaterial.SetTexture("_MainTex", Resources.Load(type, Texture2D));
But all I got was a null reference exception. Can anybody point out as to what I'm doing wrong here?
Answer by kirpigiller · Jun 01, 2011 at 02:21 PM
Is that sharedMaterial created? sharedMaterial is null?
Check you have implemented smth like this::
MeshRenderer mr = tempFace.GetComponent(MeshRenderer) as MeshRenderer; mr.sharedMaterial = new Material(Shader.Find("MyShader")); mr.sharedMaterial.SetTexture(.....
Yes this works. The materials are no longer being instantiated, and are being shared ins$$anonymous$$d. Now the only thing left is to figure out why dynamic batching is still not working.
Did the batching work when you had just one material for all the batched objects? because i eventually got batching working first as a test with just one texture on all objects without setting shared texture, but when i change the offset and scale of the texture on individual meshes it doesnt batch.
Your answer
Follow this Question
Related Questions
How to i share material and texture 1 Answer
How to make sharedMaterial work when I duplicate an object? 1 Answer
batch textures, shared materials, instantiation, sharedmaterial array 1 Answer
Why isn't (.)sharedMaterial sharing this change to "_EmissionColor"? 0 Answers
Assigning sharedMaterial to object built at runtime 2 Answers