Render.material.color doesnt work
public static void ChangeColor(GameObject obj, Color color, bool Randomize=false)
{
if (!Randomize)
{
obj.transform.GetComponent<MeshRenderer>().material.color = color;
Debug.Log("Color Changed!: " + obj.name+ " "+color);
}
else
{
color =new Color(Random.value, Random.value, Random.value);
obj.transform.GetComponent<MeshRenderer>().material.color = color;
Debug.Log("Color Changed Randomly!: " + obj.name+" "+ color);
}
}
I have the following code and when I run the program, the logs show up but the color does not change, what am I missing?
Answer by theCodeHermit · May 24, 2020 at 06:31 AM
Maybe this will work ?
Renderer rend = gameobject.GetComponent<Renderer>();
rend.SetColor("_BaseColor", color);
https://i.ibb.co/qghZj88/s-$$anonymous$$.gif
Try something like this then. By creating new temporary material and using shared$$anonymous$$aterial you are making a material instance. So other objects using the same material wont be affected. I am really not an expert so its better to read more on the renderer functions. Also this time I used _Color ( I was on lwrp before and I think they used different names for the color) https://docs.unity3d.com/ScriptReference/Renderer-shared$$anonymous$$aterial.html https://docs.unity3d.com/ScriptReference/Renderer.html
public GameObject obj;
public Color color;
public void ChangeColor(){
Renderer rend = obj.GetComponent<Renderer>();
$$anonymous$$aterial temp_$$anonymous$$at = new $$anonymous$$aterial(rend.shared$$anonymous$$aterial);
temp_$$anonymous$$at.SetColor("_Color", color);
rend.shared$$anonymous$$aterial = temp_$$anonymous$$at;
}
private void Start() {
ChangeColor();
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612235119im_/https://answers.unity.com/themes/thub/images/avi.jpg)