- Home /
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!
Thank you for the answer, and I have kept it in $$anonymous$$d. However, I have taken some coding tutorials for JavaScript and I have a good knowledge of GUI stuff and more in unity coding. I just can't get around some problems every once in a while, and I come here for help because my school is relying on me to build a good game that they can learn from in ICT. I just need help with a couple of things. All I'm asking for is a little help with the script I have now... please...
The problem, though, is that you are asking for help with things that are at the very fundamental level of program$$anonymous$$g. Not just Unity program$$anonymous$$g, or Java Script program$$anonymous$$g, but generic program$$anonymous$$g concepts.
You are currently struggling with how to set a property on an object, for example. Assignment of values to variables is one of the (if not the) most basic of concepts in any language.
Beyond that, your question touches on concepts related to statics and conditional statements, both of which should be covered early on in any set of tutorials or lessons.
I truly apologise about this. However it isn't the variables that I'm struggling with. It's applying it to the GUI Button as I need it. I promise. An answer to this, and I'm gone. Please, I promise...
I understand that my knowledge is limited, but my game has gone well so far. All I'm asking for is a little bit of help with this one bit, then I'm gone. I swear, I just need help. Just one script, or guidance on how to use the script. I know how it works I just can't figure out how to put it in a script.
Hmm... Ok. Thanks so much! After reading that comment I was able to piece together a system for my game that allows the user to choose from a variety of colours to colour there GUI. Also, I admit that my knowledge is limited. I know that. However I was able to think of a good custom system for the menu itself after just getting my question closed three times. But my friend. You have been incredibly helpful. Thank you!
Answer by FreeTimeDev · Oct 30, 2014 at 01:45 AM
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
}
}
Thanks! I've been able to use that help and previous help and I've made a custom script!