Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 ShinyTaco · Sep 12, 2012 at 07:44 AM · guitimerstopcountdown

How to stop a timer with GUI button?

Hello,

The timer starts fine.

I'm trying to stop a timer using a GUI button

 function OnGUI () {
 
     GUI.Label(new Rect(300, 100, 100, 20), (elapsedTime.ToString()));
  
     if (!hasBeenPressed)
     
     {    
         //BLAH BLAH BLAH
                    
                    
                    
                    //STOP TIMER HERE
                         stopTime = Time.time; //THESE LINES DON'T GET CALLED
                         timeActive = false;       //What am I doing wrong?
                    
 
         }
         
         
         if (hasBeenPressed) {
         
         //DO SOMETHING
         
 
         }
     }
 
 }

` `I started the timer in the update method when the jump button is fired.

I'm trying to stop it using a GUI button, which is posted above.

I have the following variables declared:

 public var startTime : float; 
 public var elapsedTime : float;     
 public var stopTime : float; 
 public var timeActive : boolean; 

but I cannot figure out how to use them when the GUI button is fired.

My aim is to use the GUI button to stop the timer.

Any help or guidance in the right direction would be great.

Thanks.

Edit to add I need to get the stopTime so that I can pass it on later. The timer is counting up from 0 when the jump button is fired. The timer needs to stop when the GUI button is pushed - as seen in the code above.

So, in a nutshell.

Button A needs to start the counting up timer. Button B needs to stop the counting up timer.

Comment
Add comment · Show 3
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 AlucardJay · Sep 13, 2012 at 08:24 AM 0
Share

From what I can glean you wish to manipulate a whole bunch of variables after a GUI.Button event. Check the API for GUI.Button : http://docs.unity3d.com/Documentation/ScriptReference/GUI.Button.html

Also, my answer here works with GUI Buttons, it also has a boolean to change what GUI elements are shown : http://answers.unity3d.com/questions/231521/how-to-display-data-in-an-array.html

avatar image ShinyTaco · Sep 17, 2012 at 12:54 AM 0
Share

agoalucardj, thank you very much. That is what I was after.

avatar image AlucardJay · Sep 17, 2012 at 01:00 AM 0
Share

I just converted my comment to an answer then =]

1 Reply

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

Answer by AlucardJay · Sep 13, 2012 at 10:59 AM

Your script doesn't show the most crucial part ... what is happening if !hasBeenPressed and before //STOP TIMER HERE ? This is the GUI.Button, yes? I posted links that should help with this, but you seem to just be asking for script to start and stop a timer with 2 buttons. This is easy :

  Update() {
       if (timeActive)
            elapsedTime += Time.deltaTime;

  GUI() {
  
       if (!timeActive) {
  
            GUI.Button(Start) {
                 startTime = Time.time;
                 timeActive = true;
  
  
       if (timeActive) {
  
            GUI.Button(Stop) {
                 stopTime = Time.time;
                 timeActive = false;

  
  
  
  
  
  

Also, my answer here works with GUI Buttons, it also has a boolean to change what GUI elements are shown : http://answers.unity3d.com/questions/231521/how-to-display-data-in-an-array.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

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

11 People are following this question.

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

Related Questions

C# countdown timer 9 Answers

A node in a childnode? 1 Answer

Problems in c# countdown timer 1 Answer

Disable the function timer? 1 Answer

how do I make a Time Based Score System? 1 Answer


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