Button that changes a colour.

So I understand that I just asked for how to store a GUI colour in a variable, but I couldn’t get it to work in the way I needed it to.

I wanted a button that changes it’s colour if you click on it. Here’s what I tried:

function OnGUI () {
GUI.color = Color.green;
var colourGreen : Color = GUI.color;

if(GUI.Button(Rect(Screen.width/1.8, Screen.height/1.8, 100, 50), "Green"))
    {
	colourGreen = true;		
    }

}

Unfortunately this just changes the button from the start. Also, this should work for all of the buttons I have. I just used one button so that it would shorten the question. Hehe!

I understand that this should return an error, but I hope that you all understand what I’m trying to do, so if anybody knows how I can do this, then please show me in the answers section! Thanks!

Make another button.

Let’s say you have your default and not selected button – let’s call this button A.

When user clicks button A, replace it with button B. Button B will be green/red/blue or what ever you want. Note the bool bIsGreen.

if(!bIsGreen) //if bIsGreen is false...
{
if(GUI.Button(Rect(Screen.width/1.8, Screen.height/1.8, 100, 50), "Not Green")) //and you click a button
     {
     colourGreen = true;   //make it green!
     bIsGreen = !bIsGreen
     }
}else{ //...else (if we are green already)
if(GUI.Button(Rect(Screen.width/1.8, Screen.height/1.8, 100, 50), "Green")) 
     {
     colourGreen = false;   //make it not green!
     bIsGreen = !bIsGreen
     }
}