Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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.

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

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges