Change object color

Hi
I’d like to change an object color when my player collides with it, and I want it to go back to its original color when I collide with it again.
For now I did this :

using UnityEngine;
using System.Collections;

public class colorChange : MonoBehaviour
{
    public Material color1;
    public Material color2;


    void OnTriggerEnter(Collider other)

    {
        print(other.name);
        if (other.name == "Player")

        {
          
                GetComponent<Renderer>().material.color = Color.green;
                Debug.Log("Player Collided with CUBE");
           
          
        }
    }
}

And it works fine to change the color once, but what should I add to change its color back when I touch it again?

Thanks a lot

public class colorChange : MonoBehaviour
{
    public Material color1;
    public Material color2;

    void OnTriggerEnter(Collider other)
    {
        print(other.name);
        if (other.name == "Player")
        {
            GetComponent<Renderer>().material.color = Color.blue;
            Debug.Log("Player Collided with CUBE");
        }
        else {
            GetComponent<Renderer>().material.color = Color.green;
        }
    }
    void OnTriggerExit(Collider other)
    {
        if(other)
        GetComponent<Renderer>().material.color = Color.blue;
        Debug.Log("Player Exit");      
    }
}

i hope it helps what you need :slight_smile:

Thanks but in this case my cube goes back to red as soon as I exit the cube. I would like it to stay green after I exit, and go back to red if I touch it again.

The script is atached to your player o the box?

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class colorChange : MonoBehaviour
{
    public Material color1;
    public Material color2;
  
    void OnTriggerEnter(Collider other)
    {
        print(other.name);
        if (other.name == "Player")
        {
            // green on trigger
            GetComponent<Renderer>().material.color = Color.green;
            Debug.Log("Player Collided with CUBE");
        }
        else {
            //  red if trigger
            GetComponent<Renderer>().material.color = Color.red;
        }
    }
    void OnTriggerExit(Collider other)
    {
        if (other.gameObject.tag == "Player")
        {
            // Stay green after exit on trigger
            GetComponent<Renderer>().material.color = Color.green;
            Debug.Log("Player Exit");
        }
    }
}

It’attached to the box

It’s close!
With this I can have my red cube turn green after I enter AND exit the cube (wich is fine for me), and then turn it red back when I enter it again, but it’s still turning green when I exit…

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class colorChange : MonoBehaviour
{
    public Material color1;
    public Material color2;

    void OnTriggerEnter(Collider other)
    {
        print(other.name);
        if (other.name == "Player")
        {
            // green on trigger
            GetComponent<Renderer>().material.color = Color.red;
            Debug.Log("Player Collided with CUBE");
        }
        else
        {
            //  red if trigger
            GetComponent<Renderer>().material.color = Color.green;
        }
    }
    void OnTriggerExit(Collider other)
    {
        if (other.gameObject.tag == "Player")
        {
            // Stay green after exit on trigger
            GetComponent<Renderer>().material.color = Color.green;
            Debug.Log("Player Exit");
        }
        else
        {
            GetComponent<Renderer>().material.color = Color.red;
           
        }
    }

}

In line 16, 22, 30 and 35 above, do not set the color of the material.

Instead, set the actual material to either color1 or color2, your public inspector variables that you appear not to be using.

GetComponent<Renderer>().material = color1;
1 Like

From your original description, I’m not sure why you’re messing with the color when exiting the trigger. I thought you said you just want to toggle back and forth between two colors whenever the player collides with it.

Something like:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class colorChange : MonoBehaviour
{

    void OnTriggerEnter(Collider other)
    {
        print(other.name);
        if (other.name == "Player")
        {
            Renderer myRenderer = GetComponent<Renderer>();
            if (myRenderer != null)
            {
                if (myRenderer.material.color == Color.red)
                {
                    myRenderer.material.color = Color.green;
                }
                else
                {
                    myRenderer.material.color = Color.red;
                }
            }
            Debug.Log("Player Collided with CUBE");
        }
        else
        {
            Debug.Log("CUBE trigger called by non-player");
        }
    }
}

Again thanks a lot to both of you. It does now what I wanted.
Thanks

2 Likes

Oh and one last thing, what if I want to use a custom color with rgb or hsv? (instead of green/red)

1 Like

You can make any color with new Color( r, g, b);:

.red and .green are just presets, as listed in the above docs.

1 Like

Personally I prefer just creating a public Color field and in the inspector using the cool little color wheel to pick the color. Unless I already have specific values for r, g, b in mind. :stuck_out_tongue:
(I know you know all this, just for the OP)

public Color KindaRedishColor;  //Find these fields in the inspector, click on them, and use the tool to design your custom color
public Color KindaGreenColor;
//Change to Red
myRenderer.material.color = KindaRedishColor;
1 Like