Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by KnightRiderGuy · Nov 28, 2015 at 07:45 PM · c#uitimercountdowntimer-script

Start and Stop Timer Help

My code is a little messed up, the timer works but I need to add a start and stop button?

 using UnityEngine;
 using System.Collections;
 using UnityEngine.UI;
 
 public class timer : MonoBehaviour {
 
     private static timer _instance ;
 
     public Text timerLabel;
     
     private float time;
 
     /*void Awake() {
         DontDestroyOnLoad(transform.gameObject);
     }*/
 
     //Begin New
     void Awake()
     {
         //if we don't have an [_instance] set yet
         if(!_instance)
             _instance = this ;
         //otherwise, if we do, kill this thing
         else
             Destroy(this.gameObject) ;
         
         
         DontDestroyOnLoad(this.gameObject) ;
     }
     //End New
     
     void Update() {
 
         
         time += Time.deltaTime;
 
         var minutes = Mathf.Floor(time / 60);
         var seconds = time % 60;//Use the euclidean division for the seconds.
         var fraction = (time * 100) % 100;
         
         //update the label value
         timerLabel.text = string.Format ("{0:00} : {1:00} : {2:00}", minutes, seconds, fraction);
     }
 
     //Reset Timer
     public void  ResetTimer(){
         time = 0;
         Debug.Log("Timer Reset");
     }
 
     //Stop Timer
     public void  StopTimer(){
         //Stop Timer Here
 
         Debug.Log("Timer Stopped");
     }
 }
 
     
 
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

1 Reply

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

Answer by Runalotski · Nov 28, 2015 at 08:30 PM

Add a bool pauseTimer and an if statment on line 35 make it

 If(!pauseTimer)
     time += time.deltaTime;
 

Now when you set this bool to true the time variable will not be incremented

Comment
Add comment · Show 7 · 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 KnightRiderGuy · Nov 28, 2015 at 08:45 PM 0
Share

What about a Stop button and a separate start Button?

 //Reset Timer
     public void  ResetTimer(){
         time = 0;
         Debug.Log("Timer Reset");
     }
 
     //Stop Timer
     public void  StopTimer(){
         //Stop Timer Here
 
         Debug.Log("Timer Stopped");
     }
 
     //Start Timer
     public void  StartTimer(){
         //Start Timer Here
         
         Debug.Log("Timer Started");
     }


avatar image LazyElephant · Nov 28, 2015 at 09:06 PM 0
Share

In StopTimer you would just have to set pauseTimer to true, then set it to false in StartTimer.

avatar image KnightRiderGuy LazyElephant · Nov 28, 2015 at 09:14 PM 0
Share

Runalotski had mentioned add a Bool but I'm not sure I follow where or what?

avatar image LazyElephant · Nov 28, 2015 at 09:45 PM 0
Share

You would add the bool as a member variable for your timer class. Initialize it to false and then at line 35, modify the code to add the if statement.

avatar image KnightRiderGuy LazyElephant · Nov 28, 2015 at 11:04 PM 0
Share

I think I'm not getting it still?

 using UnityEngine;
 using System.Collections;
 using UnityEngine.UI;
 
 public class timer : $$anonymous$$onoBehaviour {
 
     private static timer _instance ;
 
     public Text timerLabel;
     public bool pauseTimer = false;
 
     private float time;
 
     /*void Awake() {
         DontDestroyOnLoad(transform.gameObject);
     }*/
 
     //Begin New
     void Awake()
     {
         //if we don't have an [_instance] set yet
         if(!_instance)
             _instance = this ;
         //otherwise, if we do, kill this thing
         else
             Destroy(this.gameObject) ;
         
         
         DontDestroyOnLoad(this.gameObject) ;
     }
     //End New
     
     void Update() {
 
         if(!pauseTimer)
         time += Time.deltaTime;
 
         var $$anonymous$$utes = $$anonymous$$athf.Floor(time / 60);
         var seconds = time % 60;//Use the euclidean division for the seconds.
         var fraction = (time * 100) % 100;
         
         //update the label value
         timerLabel.text = string.Format ("{0:00} : {1:00} : {2:00}", $$anonymous$$utes, seconds, fraction);
     }
 
     //Reset Timer
     public void  ResetTimer(){
         time = 0;
         Debug.Log("Timer Reset");
     }
 
     //Stop Timer
     public void  StopTimer(){
         //Stop Timer Here
 
         Debug.Log("Timer Stopped");
     }
 
     //Start Timer
     public void  StartTimer(){
         //Start Timer Here
         
         Debug.Log("Timer Started");
     }
 }
 
     
 
avatar image LazyElephant KnightRiderGuy · Nov 28, 2015 at 11:14 PM 0
Share

Ok, now you should only have to add the stuff I wrote in my first comment. In StopTimer(), add tbe line: pauseTimer = true. In StartTimer(), add the line: pauseTimer = false. You should then be able to start and stop the timer with your two functions.

Show more comments

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

46 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Count Down Timer C# Conversion Script Not Working Help 3 Answers

Countdown timer... 2 Answers

I need a "loop" timer that disables a action until the timer resets. 0 Answers

Toggle Button For Timed Device When Timer Runs Out 1 Answer

How to get a countdown timer with values entered by the player 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