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 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.
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’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;
}
}
}
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");
}
}
}
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.
(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;