Outdated Script Tutorial needs an update

Hi Unity Gurus! I’m new to all this so be gentle with me. I’m working through the site’s tutorials. They’ve been great so far but I’ve moved on to the scripting section only to find Unity4.* outdated code. For example…

using UnityEngine;
using System.Collections;

public class ExampleBehaviourScript : MonoBehaviour
{
    void Update ()
    {
        if(Input.GetKeyDown(KeyCode.R))
        {
            gameObject.renderer.material.color = Color.red;
        }
        if(Input.GetKeyDown(KeyCode.G))
        {
            gameObject.renderer.material.color = Color.green;
        }
        if(Input.GetKeyDown(KeyCode.B))
        {
            gameObject.renderer.material.color = Color.blue;
        }
    }
}

The correct way I’ve found with some searching the documentation is as follows…

using UnityEngine;
using System.Collections;

public class Color : MonoBehaviour

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

However, the terms “red” , “green” and “blue” are returning the error - CSO117: “Color” does not contain a definition for “red”.

Considering the updated code was a copy and paste I’m assuming I’ve missed something simple in all my newb glory. To any replies thanks in advance!

The problem is that you’re calling your class Color, which replaces the existing Color class which is where red and such are defined. So when you use Color.red, the compiler is seeing your Color class and looking for a red property in it, and it doesn’t exist.

The arguably correct thing to do is to change your class to something besides Color. But if that isn’t what you want to do then you need to point specifically to Unity’s Color class…

...material.color = UnityEngine.Color.red;