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
1
Question by rondonron · Oct 14, 2014 at 07:59 PM · buttonbuttons

How to detect a button being released?

I am using JavaScript and I have a GUI button. When the button is held down a timer goes and when it is released the timer stops. I'd like to have it so When the mouse releases the button the game would stop, show a game over and your time would be displayed.

I tried GetButtonUp function but that doesnt work with GUI. Is there a way to make buttons without GUI ? pragma strict

 var counter: int = 0; 
 var Timer = 0.0;
 var gameText: GUIText;
 var btnStyle:GUIStyle;
 var backGround:GUIStyle;
 
 function Start(){ 
     gameText = gameObject.GetComponent(GUIText); 
 }
 function OnGUI () {
 GUI.Box (Rect (100,100,200,300), "Hold IT", backGround); 
     if (GUI.RepeatButton(new Rect(50,50,100,110),"", btnStyle)) {
     counter++;
     Timer += Time.deltaTime;
     var hours : int =  Timer / 3600;
     var minutes : int = Timer / 60;
     var seconds : int = Timer % 60;
     var fraction : int = (Timer * 100) % 100;
     gameText.text = String.Format ("{0:00}:{1:00}:{2:00}:{3:00}", hours, minutes, seconds, fraction);  
     }
   }  
Comment
Add comment · Show 2
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 MrSoad · Oct 14, 2014 at 11:54 PM 0
Share

You could use a GUITexture with a 2DBox collider on it. Then you could use the On$$anonymous$$ouseDown() and On$$anonymous$$ouseUp() functions to create your own button rather than using the OnGUI() function's inbuilt buttons.

avatar image Kiwasi · Oct 15, 2014 at 03:02 AM 0
Share

Another alternative is to use a third party GUI system. Unity UI (available in the 4.6 beta) and NGUI (Asset store) are both popular options

1 Reply

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

Answer by Fmressel · Oct 14, 2014 at 11:55 PM

If I'm understanding your question correctly, I would do something like this:

 var counter: int = 0; 
 var Timer = 0.0;
 var gameText: GUIText;
 var btnStyle:GUIStyle;
 var backGround:GUIStyle;
 
 //A bool to let us know if the button has been pressed or not
 var down = false;
  
 function Start(){ 
     gameText = gameObject.GetComponent(GUIText); 
 }
 function OnGUI () {
 GUI.Box (Rect (100,100,200,300), "Hold IT", backGround); 
     if (GUI.RepeatButton(new Rect(50,50,100,110),"", btnStyle)) {
     //when the button is pressed we make down equal true
     down = true;
     counter++;
     Timer += Time.deltaTime;
     var hours : int =  Timer / 3600;
     var minutes : int = Timer / 60;
     var seconds : int = Timer % 60;
     var fraction : int = (Timer * 100) % 100;
     gameText.text = String.Format ("{0:00}:{1:00}:{2:00}:{3:00}", hours, minutes, seconds, fraction);  
     }
     else if(down == true && Event.current.type == EventType.MouseUp) { 
            //An else if statement that will only be true if the button is not 
            //pressed but has been pressed in the past and the mouse has been lifted
            //but to check in the OnGUI function if the mouse has lifted we need to use Event.current and 
            //check it against the event type of mouse up which will only be true if the mouse has been pressed down and released
            //Thanks to BoredMormon for the Event.current == EventType.MouseUp part! This code wouldn't work without it!

            //Game over code
            //Remember to set down to false every time a new game is started or else
            //the game over code will run right at the start of the game!
            //Also remember that if you don't set down to false after the game over code every time you click the mouse and release the game over code will run
     }
   }  

Here is some more info about Events:
http://docs.unity3d.com/ScriptReference/Event.html

Hope that helps! Let me know if it didn't answer your question!

Comment
Add comment · Show 3 · 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 Kiwasi · Oct 15, 2014 at 02:56 AM 1
Share

Just note that this will not work using OnGUI as written. The immediate mode GUI framework calls OnGUI multiple times per frame, with various events. You need to implement event checking for this to work.

Change line 25 to the following for this code to work

 else if(down == true && Event.current == EventType.$$anonymous$$ouseUp) 
avatar image rondonron · Nov 07, 2014 at 05:30 PM 0
Share

Sorry for the late response ! So I added the new line of code for the event but I get this error

Assets/GUITest.js(25,43): BCE0051: Operator '==' cannot be used with a left hand side of type 'UnityEngine.Event' and a right hand side of type 'UnityEngine.EventType'.

avatar image Bunny83 · Nov 07, 2014 at 06:07 PM 0
Share

@rondonron: There's a typo ;) It should be:

 else if(down == true && Event.current.type == EventType.$$anonymous$$ouseUp) { 

I'll fix it in the answer...

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

I want the gameobject field of my persistent listeners to reference an object other than the one they are on. Is this possible? 1 Answer

How can I stop getting mouse position of Ui Elements? 0 Answers

rewrite script 1 Answer

[Unity 4.6] UI Button problem null reference exception [Js] 0 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