- Home /
On Clicked, On Released GUI Button ?
Hi all,
I want to make that my variable equals 1 only when my button clicked, and when it released it will be 0. I'm using javascript.
Thanks in advance for help :)
Answer by GameGuy · Apr 14, 2012 at 02:11 PM
You can use GUI.RepeatButton for things like that.
http://unity3d.com/support/documentation/ScriptReference/GUI.RepeatButton.html
Answer by fafase · Apr 14, 2012 at 04:11 PM
GetKey sends 1 on all frames you keep pressing.
GetKeyDown sends 1 only on the frame you press, and 0 the rest of the time even if you hold down
GetKeyUp sends 1 when you release the button, and 0 any other time.
GetMouseButton and GetButton work the same way.
So I guess you are looking for
var anyVar: boolean;
function Start(){
anyvar = false;
}
function Update(){
if(Input.GetKeyDown("Jump")
anyvar = true;
else if(Input.GetKeyUp("Jump"))
anyvar = false;
}
Jump is the space bar by default, check Edit->Project Setting->Input to see what is what and change as you wish.
Or more simply:
var anyVar: boolean;
function Start(){
anyvar = false;
}
function Update(){
if(Input.GetKey("Jump")
anyvar = true;
else
anyvar = false;
}
you choose.
you are correct the getkey() is the function to use but you do not need to use with a boolean expression you can set it as anyvar =0 and when you press space it will hold it to 1 and on release back to 0. If true false is what he wants then a simple copy and paste your code would work perfect.
You could as well.The thing is whether you use boolean or int or even enum{Not_Ok;Ok;} is the same. True is 1 and false is 0. It is just various way.
Your answer
Follow this Question
Related Questions
GUI Button Disappearing 1 Answer
Javascript GUI.Button help, Error BCE0077 1 Answer
NGUI Repeat button In JavaScript problem 1 Answer
GUIText Problem With MENU 1 Answer
Gui Button Solid 2 Answers