renderer.material function wont work.

I’ve been following a Unity tutorial published by unity word for word but some of his code is giving me errors. It’s probably the new version or something but I need some help. Here’s my code.
using UnityEngine;
using System.Collections;

public class ColorChanger : MonoBehaviour {

	// Use this for initialization
	void Start () {
	
	}
	
	// Update is called once per frame
	void Update () {
		if (Input.GetKeyDown (KeyCode.R)) {
			gameObject.renderer.material.color = Color.red;
		}
	}
}

The material.color is highlighted red and it give me errors. Why?

Use GetComponent< Renderer >.

its up to which version of unity your are using, if its unity 5 than its like

 void Update()
    {
        if (Input.GetKeyDown(KeyCode.R))
        {
            gameObject.GetComponent<Renderer>().material.color = Color.red;
        }
    }