When I judge if two materials are the same, but the result is forever false even though the materials are the same

I use “==” to judge the two materials are the same.
198236-2022-08-01-084202.png

The log is printing that the material that is getting passed as a parameter to your method is an instance of a material. You get a material instance when retrieving the “material” property of a renderer. If you want to check if your objects share the same material you should use the “sharedMaterial” property of the renderers in question.

So my guess is that wherever you call the GetSoundByMaterial function you are passing the material property of a renderer instead of the sharedMaterial as a parameter?


This thread covers the difference between the material and the sharedMaterial properties: https://forum.unity.com/threads/confused-about-material-instances.402037/

You can just check if the name is contained in the scene material. As the material is a instance of the “prefab” material, it’s not equal to, and its name will have the “(Clone)” at the end

if (SceneObject.GetComponent<Renderer>().material.name.Contains("PrefabMaterial.name))