Check What Material is Being Used?

Hey guys, I’ve been trying to do something which should’ve been simple, but apparently I’m missing something.

So I was using, “if (renderer.materail.mainTexture == myTextureVar)” to see if a texture is being used, to do something. Which worked fine. But it brought my draw calls from 13 to 24. I’m still in alpha with this project, so I’m afraid if I “allow” that to be, things will get out of hand by the time I say it’s finished.

Anyways, so I figured out that changing the texture at runtime causes a new draw call, so I switched to the shader/material, and that kept my calls low.

But when I do “if (renderer.material == myMaterialVar)” then it doesn’t work.

I even debug the material used, and here’s where it gets weird. Instead of just debugging the “Material used is: myMaterialVar”, it writes, “Material used is: myMaterialVar (Instance) (UnityEngine.Material)”.

What do I have to script to check what material is being used if unity makes an instance of it and all other kinds of nonsense? And is there any documentation on this strange issue?

P.S. I write in javascipt

Use sharedMaterial instead. Accessing material creates an instance. This is done for the sake of making things easier to use, where you can do stuff like, say, “renderer.material.color =” to change the color of one specific object to blue, without having to create and manage material instances yourself. The downside is that you lose control over exactly what’s going on. (Also, you’re more likely to have misunderstandings about how Unity and materials actually work.)

So to maintain complete control you should never touch material, and only use sharedMaterial. It can be more work, but you can still do everything that you could with material.