Evaluating Material not working?

Hi guys, so this is my question of the day:

I need to do something based on the material of the object, lets suppose I have 100 planes, each of them has the same material, except one so I need to check on every plane, if it has a different material applied then i will do something, I tried something like this but It just didn’t worked. (I don’t know why)

var diffMaterial: Material; //Material of the bombs

if(gameObject.renderer.material == diffMaterial){
Debug.Log ("This object has a different Material");
//Do stuff
}

Whenever you access the .material property, Unity will create a copy / instance of the material (unless it already has beed created). Each object has it’s own material instance.

However there is the sharedMaterial / sharedMaterials property which returns the reference to the “real” material. This can be compared with your original material reference.

Try this:

(gameObject.renderer.material.name == diffMaterial.name