Trivial question from newbie

Hello Ladies and Gentlemen,
I decided to learn scripting in Unity. I started from this tutorial:

I wanted to play with this, but… now, it’s unity 5, few api changes. Script is:

if(Input.GetKeyDown(KeyCode.R))
{
gameObject.renderer.material.color = Color.red;
}

compiler shouts: You need to use GetComponent instead. My question is , how to do this ?
I tried:
gameObject.GetComponent<Renderer>().material.color = Color.black;

and now it shouts: Unexpected token: ).

Please help me :slight_smile: I know it’s trivial question

Found it, it should be:

gameObject.GetComponent.<Renderer>().material.color = Color.black;

Yeah, that is the way to do it. The .renderer is the old way of doing it. It is still there but will probably be removed in the future.

Good luck learning Unity!

1 Like

fyi: when you post code into the forum you should use [ code] [/ code] tags, it really helps with the readability, there is a sticky on them at the top of the scripting forum.

2 Likes

You can also drop the gameObject if you like.

1 Like

Done :slight_smile: