- Home /
How to check if an objects current material is the correct one?
what i'm trying to do is to check if an object's material is the "orange" material i created.
public Material orange;
public Material thisMaterial;
// Start is called before the first frame update
void Start()
{
thisMaterial = GetComponent<MeshRenderer>().material;
}
// Update is called once per frame
void Update()
{
Debug.Log(thisMaterial);
Debug.Log(orange);
}
private void OnMouseDown()
{
if (thisMaterial == orange) //if current sprite is the neutral one
{
Debug.Log("material is orange");
}
}
In the log they both show up as orange:
When I click the object, the "material is orange" message doesn't show up in the console. What am i doing wrong?
Answer by hsnzkg · Oct 05, 2021 at 02:45 AM
public Material prefabMat;
private Material singleInstanceMat;
private Material sharedInstanceMat;
private void Awake()
{
//singleInstanceMat= gameObject.GetComponent<MeshRenderer>().material;
sharedInstanceMat = gameObject.GetComponent<MeshRenderer>().sharedMaterial;
}
private void Start()
{
Debug.Log(prefabMat == sharedInstanceMat ? true : false);
//Debug.Log(prefabMat == singleInstanceMat? true : false);
}
Here is the tip if you do not acsess the singleInstanceMat there is ok the sharedMaterial is keeping the material that you want. And the first debug returns TRUE. But when you acsess the normal .material the shared material is changeing i guess because of the instance.
I hope it will help
Your answer
Follow this Question
Related Questions
Material doesn't have a color property '_Color' 4 Answers
Changing two different objects renderer colour 1 Answer
Change the material of a lightmapped object 1 Answer
MeshRenderer.material.color not changing inside if statement but is outside if statement? 1 Answer
Why isn't the color changing? 1 Answer