• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by VincentRodriguez · Apr 14, 2012 at 01:55 PM · javascriptguibuttonvariables

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 :)

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
1

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image bodec · Apr 14, 2012 at 05:33 PM 0
Share

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.

avatar image fafase · Apr 14, 2012 at 05:40 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Use a key to open a GUI? 2 Answers

Why does it give me an error in this small java script for sprint? 1 Answer

Key for GUI.Button 2 Answers

Best way to create large amount of buttons 2 Answers

How to access a non static variable by another static variable? 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges