- Home /
How to toggle a box color on & Off?
Hello guys,
Im completely new to programming, and I've been trying for days to get a cubes color to turn from white to red at the press of the T button, and to turn from Red to White at the press of the T button again. Can someone tell me where i'm going wrong here is my code that I've tried. Im currently trying to learn C#.
public class colortogglesimple : MonoBehaviour {
void Start (){
}
void Update(){
if (Input.GetKeyDown ("t"))
renderer.material.color = Color.red;
else
renderer.material.color = Color.white;
}
}
Answer by YoungDeveloper · Nov 30, 2014 at 09:52 PM
Something like this should work.
Color lastColor = Color.white;
void Start(){
renderer.material.color = lastColor;
}
void Update(){
if(Input.GetKeyDown(KeyCode.T)){
if(lastColor == Color.red) lastColor = Color.white;
else lastColor = Color.red;
renderer.material.color = lastColor;
}
}
Thank you this is Awesome and it worked!
:( it makes me sad that i couldn't figure it out on my own... how long have you been program$$anonymous$$g for? and any advice or tips?
Only practice makes perfect, learn by doing. Be sure to check question as answered.
Your answer
Follow this Question
Related Questions
Collision issue 1 Answer
How to create shared color channels 1 Answer
Problem Changin Standard shader Alpha Channel 0 Answers
How to change colors in turn? 1 Answer
Change Color of Material in Loop 1 Answer