unity get all materials on gameobject code example
Example: unity get all materials on gameobject
public class GetListOfMaterials : MonoBehaviour
{
public Renderer rend; //Drag and drop the whole [Mash Renderer] Component into this field in the inspecter
public Material originalMaterial; //Assign the material that you want to get in the inspecter
private Material material; //The target material you want to get
void Start()
{
List m = new List(); //Create a list for all materials
rend.GetMaterials(m); //Gets a list of all materials on this local object
//m is now the list of all materials
//===========Get Specific material by name from this list===========
foreach (Material mat in m)
{
//The local material name ends with " (Instance)", so we need to get rid of that
string matName = mat.name.Replace(" (Instance)", "");
if (matName == originalMaterial.name)
{
material = mat;
}
}
}
//Your Welcome :D
}