Search Unity

  1. Unity 2019.1 is now released.
    Dismiss Notice

Beginner Scripting - Problem with Renderer color material

Discussion in 'Scripting' started by Abellacy, May 2, 2019.

  1. Abellacy

    Abellacy

    Joined:
    Apr 3, 2019
    Posts:
    2
    So as the title says, I´m learning about beginner scripting on Unity(trying to follow scripting tutorials and documentation) and I reached a point where there is a class like this(that one is literally the one from the Unity explanation):

    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class ExampleBehaviourScript : MonoBehaviour
    6. {
    7.    void Update()
    8.    {
    9.        if (Input.GetKeyDown(KeyCode.R))
    10.        {
    11.            GetComponent<Renderer> ().material.color = Color.red;
    12.        }
    13.        if (Input.GetKeyDown(KeyCode.G))
    14.        {
    15.            GetComponent<Renderer>().material.color = Color.green;
    16.        }
    17.        if (Input.GetKeyDown(KeyCode.B))
    18.        {
    19.            GetComponent<Renderer>().material.color = Color.blue;
    20.        }
    21.    }
    22. }
    23.  


    so I tried to apply that on a Unity Scene cube, writing it like this (I only tried to add yellow):

    Code (CSharp):
    1.  
    2. public class Colors : MonoBehaviour
    3. {
    4.     void Start()
    5.     {
    6.  
    7.     }
    8.  
    9.     void Update()
    10.     {
    11.         if (Input.GetKeyDown(KeyCode.R))
    12.         {
    13.             gameObject.GetComponent<Renderer>().material.color = Color.red;
    14.         }
    15.         if (Input.GetKeyDown(KeyCode.G))
    16.         {
    17.             gameObject.GetComponent<Renderer>().material.color = Color.green;
    18.         }
    19.         if (Input.GetKeyDown(KeyCode.B))
    20.         {
    21.             gameObject.GetComponent<Renderer>().material.color = Color.blue;
    22.         }
    23.         if (Input.GetKeyDown(KeyCode.Y))
    24.         {
    25.             gameObject.GetComponent<Renderer>().material.color = Color.yellow;
    26.         }
    27.     }
    28. }
    29.  
    And it worked out surprisingly well... Until now. I just tried to access the script now for using it on another object and scene (different from the ones I used it on the 1st time) and now it gave me that error for the 4 colors:


    Colors.cs(18,72): error CS0117: 'Color' does not contain a definition for 'red'


    So i researched a bit out there in Unity Answers, and i found out I cannot name my class the same as the Unity class, which is Color. Okay, I found that logic although it didn´t cause any problems, so then I tried it modifying the class name to ColorScript, which, if I´m right, shouldn´t cause the conflict, but... :

    ColorScript.cs(18,72): error CS0117: 'Color' does not contain a definition for 'red'


    and right now I´m kinda lost on this. In the case anyone could help, thanks in advance!! If needed I can send screenshots or the script.
     
    Last edited: May 2, 2019
  2. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    2,147
    Hard to say exactly. Make sure you rename your actual file this script is in, and that there are no other errors in your project. Make sure you don't have any other remnant Color named object/script files in your project as well. Do you have the

    Failing that, you can explicitly reference Unity's Color object over any of your own Color object using the fully qualified name:
    Code (csharp):
    1.  
    2. UnityEngine.Color.red; // or yellow or blue, etc.
    3.  
    That's a bit more advanced in regards to namespaces and I don't want to overwhelm you as you're new.

    Color definitely has defined constants for certain ones, as you can see in the docs: https://docs.unity3d.com/ScriptReference/Color.html