- Home /
Button color change
WHat im trying to do is make it so that when i click on the button i want it to turn red, heres my code im not sure what wrong with it. Can anyone help me out with this. Thanks in advance.
using UnityEngine; using System.Collections;
public class GameTabs5 : MonoBehaviour {
void OnGUI(){
if (GUI.Button(new Rect(140,35,70,35), "Traps")){
GUI.color = Color.red;
}
}
}
Answer by whydoidoit · Jun 17, 2012 at 12:24 PM
OnGUI is a bit hard to get the hang of to start with :) It is called every frame, so you just set a bool true when it has been clicked and use that on the subsequent frames:
using UnityEngine;
using System.Collections;
public class GameTabs5 : MonoBehaviour {
bool buttonClicked = false;
void OnGUI(){
if(buttonClicked)
GUI.color = Color.red;
if (GUI.Button(new Rect(140,35,70,35), "Traps")){
buttonClicked = true;
}
}
}
Thanks it works really appreciate it, but when i click on 2 buttons there both red how do i make it so that the one that selected is red and when i click on another one that one lights up and the other one goes back normal.
Would this be the right code
GetComponent("GameTabs5"){ buttonClicked = false; }
Sorry to be so long getting back to you (Sunday's always busy with the family). If you want two buttons which can both be red then this:
using UnityEngine;
using System.Collections;
public class GameTabs5 : $$anonymous$$onoBehaviour {
bool buttonClicked = false;
bool button2Clicked = false;
void OnGUI(){
if(buttonClicked)
GUI.color = Color.red;
else
GUI.color = Color.white;
if (GUI.Button(new Rect(140,35,70,35), "Traps")){
buttonClicked = true;
}
if(button2Clicked)
GUI.color = Color.red;
else
GUI.color = Color.white;
if (GUI.Button(new Rect(140,35,70,35), "Traps")){
button2Clicked = true;
}
}
}
If you want the other button to always be normal just do a GUI.color = Color.white; after your if(GUI.Button) in the code above.
Yea Sundays are pretty busy for me to, but what i was asking was like for example i click the traps button it turns red, then i press the spells button i then want spells to turn red and then have traps go back to normal so that only one button is red at a time.
This should achieve that effect:
using UnityEngine;
using System.Collections;
public class GameTabs5 : $$anonymous$$onoBehaviour {
bool buttonClicked = false;
bool button2Clicked = false;
void OnGUI(){
if(buttonClicked)
GUI.color = Color.red;
else
GUI.color = Color.white;
if (GUI.Button(new Rect(140,35,70,35), "Traps")){
buttonClicked = true;
button2Clicked = false;
}
if(button2Clicked)
GUI.color = Color.red;
else
GUI.color = Color.white;
if (GUI.Button(new Rect(140,35,70,35), "Traps")){
button2Clicked = true;
buttonClicked = false;
}
}
}
You could set one onf the buttonClicked variables to true if you want it on by default.
Your answer
Follow this Question
Related Questions
Change the Colour of UGUI button in script? 2 Answers
Name of left ctrl? 1 Answer
Multiple Button Modifications 2 Answers
panel and button 1 Answer
guiTexture color half alpha White? 1 Answer