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. =)

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.

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

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