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 KnightRiderGuy · Dec 04, 2014 at 10:06 PM · timertimer countdowntimer-scripttimerscountup

Stop and Pause Timer

I found this very simple timer script that would be ideal for a trip timer but I am wondering how to add a pause, stop and reset button to it?

 #pragma strict
 private var startTime : float;
 var textTime : String;
 //First define two variables. One private and one public variable. Set the first variable to be a float. 
 //Use for textTime a string. 
 function Start() {
 startTime = Time.time;
 }
 function OnGUI () {
 var guiTime = Time.time - startTime; 
 //The gui-Time is the difference between the actual time and the start time.
 var minutes : int = guiTime / 60; //Divide the guiTime by sixty to get the minutes.
 var seconds : int = guiTime % 60;//Use the euclidean division for the seconds.
 var fraction : int = (guiTime * 100) % 100;
  
  textTime = String.Format ("{0:00}:{1:00}:{2:00}", minutes, seconds, fraction); 
 //text.Time is the time that will be displayed.
  GetComponent(GUIText).text = textTime;
  
 }
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 ahmedbenlakhdhar · Dec 04, 2014 at 10:32 PM

Your script uses Time.time which cannot be paused. So you may create your own timer instead, by adding Time.deltaTime each frame.

This script allows you to play, pause and reset the timer:

 #pragma strict
 private var time : float;
 var textTime : String;
 var timerOn : boolean;
 var buttonText : String;
  
 function Start() {
     timerOn = true;
     buttonText = "Pause";
 }
  
 function Update(){
     if(timerOn)
         time += Time.deltaTime;
 }
  
 function OnGUI () {
     var guiTime = time;
     
     var minutes : int = guiTime / 60; //Divide the guiTime by sixty to get the minutes.
     var seconds : int = guiTime % 60;//Use the euclidean division for the seconds.
     var fraction : int = (guiTime * 100) % 100;
  
     textTime = String.Format ("{0:00}:{1:00}:{2:00}", minutes, seconds, fraction); 
     //text.Time is the time that will be displayed.
     GetComponent(GUIText).text = textTime;
 
     if (GUI.Button(Rect(10,10,50,30), buttonText)){
         timerOn = !timerOn;
         if(timerOn) buttonText = "Pause";
         else buttonText = "Play";
     }
 
     if (GUI.Button(Rect(70,10,50,30), "Reset")){
        time = 0;
     }
 }

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 KnightRiderGuy · Dec 04, 2014 at 11:19 PM 0
Share

$$anonymous$$an that's awesome ahmedbenlakhdar ($$anonymous$$an that was a mouth full ;) Thanks this is exactly what I was looking all over hells half acre for :) :) :)

avatar image KnightRiderGuy · Dec 04, 2014 at 11:42 PM 0
Share

One more quick question, how would I change the simple GUI buttons to a couple of 2 state GUI texture buttons?

avatar image
1

Answer by Prasanna · Dec 05, 2014 at 07:25 AM

@zentaiguy, Try this...

 private GUIStyle TestStyle = new GUIStyle ();
 public Texture2D Pause_On = null;
 public Texture2D Play_On = null;
 
 public void OnGUI() 
 {
     if (GUI.Button (new Rect (30, 950, Pause_On.width, Pause_On.height), Pause_On, TestStyle))
     {
         //Do your Stuff's Here...
     }
     if (GUI.Button (new Rect (30, 950, Play_On.width, Play_On.height), Play_On, TestStyle))
     {
         //Do your Stuff's Here...
     }
 }

If you satisfied +1 for me.

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 KnightRiderGuy · Dec 05, 2014 at 02:04 PM 0
Share

I have been using this type of convention for most of my buttons so I'm a little confused how I make that work with this type of button configuration?

 var mouseUpTexture : Texture2D;
 var mouseDownTexture : Texture2D;
 var beep : AudioClip;
    
 
 function On$$anonymous$$ouseDown(){
      guiTexture.texture = mouseDownTexture;
      audio.PlayOneShot(beep);
 }    
 
 function On$$anonymous$$ouseUp(){
      guiTexture.texture = mouseUpTexture;
      
      yield new WaitForSeconds(0.3);
      //Do Something
 }
avatar image KnightRiderGuy · Dec 05, 2014 at 02:05 PM 0
Share

Basically I have been using one short script like this per button.

avatar image KnightRiderGuy · Dec 06, 2014 at 12:40 PM 0
Share

Still trying to figure out how to get this to work with my current GUI buttons configuration???

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

27 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

Related Questions

Timer Script Not Keeping Accurate Time? 1 Answer

How to make reverse countdown timer in unity? 1 Answer

timer not ticking down 2 Answers

How do I make a Countdown Timer in Minutes and Seconds only 1 Answer

countdown timer starts from the begining when i reLaunch the app 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