Comparing PhysicMaterials at Runtime

I’m trying to get a footfall effect during animation. I would like the footfall sound effect to be based on the material that has just been stepped on. My initial thought was to use PhysicMaterials. By adding different PhysicMaterial to the Mesh Colliders (level geometry), I was thinking I could get different effects.

I can raycast at the point the foot hits the floor and get back a RaycastHit, which has information about the collider and within the collider is the (Physic) material.

In order to translate the PhysicMaterial to a sound effect I have a table which maps the PhysicMaterial with a sound effect.

Then all I have to do is extract the collision PhysicMaterial, look it up in the table and play the associated sound effect. Or at least, that’s the theory.

The problem is that the table stores a different PhysicMaterial (different InstanceID, different name : e.g. “Gravel”) to the Mesh Collider’s PhysicMaterial (named “Gravel (Instance)”. Needless to say, both properties are populated from the same asset.

Is there any cast-iron way to compare PhysicMaterials at runtime to divine whether they came from the same source asset? Is there a class ID or GUID associated with them that would be the same in both instances? Is there any other way of comparing that might get me where I want to be without resorting to:

if ( == + " (Instance)" )


I guess the answer is “no” :slight_smile:

You can use sharedMaterial. That way you avoid the instance confusion.

Your hacky solution is working fine for me. It’s much better than tagging!