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;
  
        }

@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 :slight_smile:

Faced the same issue: “Could not resolve renderer”, this is what it works for me

Renderer myobject = this.GetComponent<Renderer>();
myobject.material.mainTexture = newtexture;