Question by
Shakir01 · Jan 06, 2019 at 07:03 PM ·
unity 5materialgetcomponentrenderergetcomponentinchildren
Gameobject Renderer not working.
I have function where I have to change the color of a gameobject in my script . I am using the renderer component of the gameobject to change the color but it's not working. I also tried with puzzle_1.GetComponentInChildren but no success. What am I doing wrong here
public GameObject puzzle_1;
public void SlotCheck()
{
puzzle_1.GetComponent<Renderer>().material.color = Color.red;
}
Comment
Answer by JeffBrin · Jan 06, 2019 at 07:10 PM
@Shakir01 Try holding this renderer in a separate renderer variable and change the colour through that. Not sure if it's the only way but it's how I do it and it works. Also make sure the puzzle_1 gameobject has a material on it already.
public GameObject puzzle_1;
Renderer rend;
Public void SlotCheck()
{
rend = puzzle_1.GetComponent<Renderer>();
rend.material.color = color.red;
}
Hope this helps :)