Change color of object on and off with click
Hi all,
I just started using unity. I am trying to make a cube change color to red on click, and then back to white once its clicked again. I am able to make it turn red upon the first click, but can't seem to make the second click work to bring it back to white. Please help :) Thanks!
#pragma strict
function Start () {
}
function Update () {
if (Input.GetMouseButtonDown(0)) {
GetComponent.<Renderer>().material.color = Color.red;
}
else {
GetComponent.<Renderer>().material.color = Color.white;
}
}
Comment
Answer by MadDevil · Oct 06, 2015 at 07:05 AM
you need to set a bool so that you can come to know if you have clicked again. use this code
public bool clicked;
void Update () {
if(Input.GetMouseButtonDown(0))
{
if(!clicked)
{
renderer.material.color = Color.red;
clicked = true;
}
else
{
renderer.material.color = Color.white;
clicked = false;
}
}
}
I'm using C# and older version of unity. so tweak it according to your use.