Checking Object Material against list of materials

I have a list of materials set in an inspector window, and I want to check several objects to see if they are using any of the materials in the list. The code I have currently looks roughly like:


However this does not work because the materials attached to the object are of a different instance. For example, if I log the names of a material in the list called 'testMaterial,' and the name of the same material when attached to an object, I get

Debug.Log(materialsList[appropriateIndex].name) //Output is 'testMaterial (UnityEngine.Material)'
Debug.Log( //Output is 'testMaterial (Instance) (UnityEngine.Material)'

Is there a way for me to show that these two materials are the same? Thanks.

You could change the name of the instance.

var myObject = instantiate(prefab, transform.position, Quaternion.identity); = "testMaterial";

Don't know if it works properly

Alright, so what I ended up doing was using string.split (reference here) to separate the strings of each material name into substrings whenever it encounters the '(' character, and then just compared the names of each material.

i.e. 'testMaterial (UnityEngine.Material)' and 'testMaterial (Instance) (UnityEngine.Material)' both become 'testMaterial ' so I can just compare the split name of the target object against the split name of each object in the list! Not quite as elegant as a simple list.contains() but works just fine :)