- Home /
Making GUI button change colour after click
Hello,
For my project, I've a list of GUI buttons, and I'd like to make a button turn green when it is activated.
When the user first starts the program, one button is already activated, thus it has to be green from the start already until the user turns it off.
I'm currently using this script; here's an excerpt:
if(GUILayout.Button("Light 1"))
{
lights[0].enabled = !lights[0].enabled;
function OnMouseDown ()
{
guiTexture.color = Color.green;
}
I've no problems with the other scripts, only when I add in the OnMouseDown function it can't compile the script. I think I'm doing it wrong, but what's the correct way of doing it?
Appreciate the help. =)
if u r using javascript ins$$anonymous$$d of writing a mousedown function just try
to check this one....
if(Input.Get$$anonymous$$ouseButtonDown(0))
{
guiTexture.color=Color.green; }
Appreciate the help; but it doesn't change the color to green. =\
Answer by Nolirneen · Mar 25, 2011 at 08:32 AM
I've found a solution to this. =)
I found this while looking around the site.
Based on that, I came up with this, and it works perfectly for me. =)
var lights : Light[];
//For the custom GUI skin
var guiSkin : GUISkin;
//For the toggle to work
var toggleBool1 : boolean = false;
//For the size and placement of the buttons
private var toggleRect1 = Rect(25, 25, 100, 20);
function OnGUI()
{ //To change the button's texture when its activated toggleBool1 = GUI.Toggle(toggleRect1, toggleBool1, "LightSwitch 1", guiSkin.button);
//To make my light respond to the toggle switch. lights[0].enabled = toggleBool1; }
Now I can make a toggle (that looks like a button) turn green when its activated, and stay green until I click it again.
For the texture of the button when its pressed I used a .png file thats just green.
PS. Thanks Duck and everyone who helped by answering also! =D
Answer by Owen-Reynolds · Mar 21, 2011 at 04:52 AM
Official way is probably to use a GUI.toggle
with a GUIstyle. In the inspector, OnNormal
is what it looks like when it is clicked on. The scripting reference has the rest. A toggle is a button the "remembers" On/Off.
OnMouseDown
is a function, so can't go inside another function. that's why it's made at you. If you really want it that way, use Input.GetButtondown(0)
(or look in the Input scripting section for other options.) But, that first if is already checking the button for you.
Hmm I tried the GetButtondown but it doesn't work; the button doesn't turn green. If I change to GUI.toggle, I'll have to change the way the switches are scripted, but I don't know how to make them work. =\
The Unity scripting section for GUI: http://unity3d.com/support/documentation/ScriptReference/GUI.html has code examples. I have it bookmarked.
Answer by Jake-L · Mar 21, 2011 at 09:30 AM
As Owenpat said, use a toggle with a button style.
buttonstate=GUILayout.Toggle(buttonstate, "Light 1","button");
That's all you need to make a Statebutton. If you want to turn it green on activation, set it's background-color:
Color c=GUI.backgroundColor; // store value
if (buttonstate)
GUI.backgroundColor=Color.green;
buttonstate=GUILayout.Toggle(buttonstate, "Light 1","button");
GUI.backgroundColor=c; // reset to old value
Sorry but, what is buttonstate? I don't understand what if (buttonstate) means, and the other parts with buttonstate in them. Tried looking around online, couldn't find any references to it.
Also, do I declare it at the start? (This toggle "Light 1" will look like a button)?
Thanks for the help. =)
buttonstate is a boolean variable you declare somewhere else. It holds the state of the button (checked or not). By calling Toggle() you create a checkbox. By providing another GUIStyle (here "button") you tell the GUI to use a different style than the usual checkboxes.
Ohhh I understand now. However, I've tried putting those lines into my script in different ways, but it keeps getting compiling errors. I think I'll have to find a way to change the way my button FUNCTIONS now to make it FUNCTION the same way as a toggle. Probably the lights[0].enabled part. Any ideas? =)
Your answer
