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 KnightRiderGuy · Dec 15, 2014 at 02:10 AM · timerguitexturetimer countdowntimer-script

Timer Pause and Reset Issue

I have this timer GUI for an air supply indicator only I'm having difficulty trying to figure out why when I pause the timer it keeps counting down even though the GUI indicator bar has stopped. Here is the code I'm using:

  var startTime : float; // (in sec)
  var timeRemaining : float; // (in sec)
  var percent : float;
  var clockBG : Texture2D;
  var clockFG : Texture2D;
  var buttonTexture: Texture2D;
  var pauseButtonTexture: Texture2D;
  var clockBGMaxWidth : float; // the starting width of the foreground bar
  
  
  function Awake()
  {
      //startTime = 20.0;
      clockBGMaxWidth = clockFG.width * 1;
  }
  
  function Update () 
  {
      if(!clockIsPaused)
      {
         // make sure the timer is not paused
         DOCountdown();
      }
  }
  
  function OnGUI()
  {
      
      var newBarWidth : float = (percent/105) * clockBGMaxWidth; // this is the width that the foreground bar should be
       
          GUI.BeginGroup (new Rect(0, 120, newBarWidth, clockBG.height));
         
         GUI.DrawTexture (Rect (118, 0, clockBG.width * 1, clockBG.height), clockBG);
         GUI.EndGroup ();
         
         
              GUI.BeginGroup (new Rect(0, 0, clockFG.width * 1, clockFG.height));
              GUI.DrawTexture (Rect (0, 0, clockFG.width * 1, clockFG.height), clockFG);
              GUI.EndGroup();
                  
                  if (GUI.Button(Rect(735,20,50,50), buttonTexture, "")){
                 Application.LoadLevel("ThreeD_Overview");
                 
                  }
                 if (GUI.Button(Rect(70,440,64,32), pauseButtonTexture, "")){
                 //Pause Timer
                 clockIsPaused = true;
                  }
      
  }
  
  function DOCountdown()
  {
      timeRemaining = startTime - Time.time;
  
      percent = timeRemaining/startTime * 100;
      if (timeRemaining < 0)
      {
         timeRemaining = 0;
         clockIsPaused = true;
         //TimeIsUp();
         Debug.Log("Time is up!");
         //Application.LoadLevel (0);
         
      }
      //ShowTime();
  }
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

Answer by MrSoad · Dec 15, 2014 at 04:21 AM

Time.time is the time since the start of the game, so even when the clock is paused the value of Time.time is increasing. So as soon as you run the DOCountdown function after a pause it will update timeRemaining with all the elapsed time since game time began, without a gap for the pause.

Take a look at this post for a very detailed description of time :

http://answers.unity3d.com/questions/9757/timetime-explanation.html

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 MrSoad · Dec 15, 2014 at 05:20 PM 0
Share

Did you initialise "timeRemaining" with your "startTime" anywhere. This change will not work unless you do that. Add this into your code :

 function Start() {
     timeRemaining = startTime;
 }

Remember that startTime must also have a value before you do this bit of code :)

avatar image KnightRiderGuy · Dec 15, 2014 at 05:47 PM 0
Share

Thanks $$anonymous$$rSoad, now THAT was a complete explanation and that worked. Now all I need to do is get the pause and reset to work.

avatar image
1

Answer by Kiwasi · Dec 15, 2014 at 06:24 AM

For a pausable timer simply change line 54 to

 timeRemaining -= Time.deltaTime;
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 16, 2014 at 06:38 PM 0
Share
 #pragma strict
  var beep1 : AudioClip;
  var beep2 : AudioClip;
  var clockIsPaused : boolean = false;
  var startTime : float; // (in sec)
  var timeRemaining : float; // (in sec)
  var percent : float;
  var clockBG : Texture2D;
  var clockFG : Texture2D;
  var buttonTexture: Texture2D;
  var $$anonymous$$iButtonTexture : Texture2D;
  var $$anonymous$$iButtonTexture2 : Texture2D;
  var clockBG$$anonymous$$axWidth : float; // the starting width of the foreground bar
  var TextureButton1: Texture;
  var TextureButton2: Texture;
  
  
  function Start() {
      timeRemaining = startTime;
  }
  
  function Awake()
  {
       TextureButton1 = $$anonymous$$iButtonTexture;
       TextureButton2 = $$anonymous$$iButtonTexture;
      //startTime = 20.0;
      clockBG$$anonymous$$axWidth = clockFG.width * 1;
  }
  
  function Update () 
  {
      if(!clockIsPaused)
      {
         // make sure the timer is not paused
         DOCountdown();
      }
  }
  
  function OnGUI()
  {
      
      var newBarWidth : float = (percent/105) * clockBG$$anonymous$$axWidth; // this is the width that the foreground bar should be
       
          GUI.BeginGroup (new Rect(0, 120, newBarWidth, clockBG.height));
         
         GUI.DrawTexture (Rect (118, 0, clockBG.width * 1, clockBG.height), clockBG);
         GUI.EndGroup ();
         
         
              GUI.BeginGroup (new Rect(0, 0, clockFG.width * 1, clockFG.height));
              GUI.DrawTexture (Rect (0, 0, clockFG.width * 1, clockFG.height), clockFG);
              GUI.EndGroup();
                  
                  if (GUI.Button(Rect(735,20,50,50), buttonTexture, "")){
                 Application.LoadLevel("ThreeD_Overview");
                 
                  }
                 if (GUI.Button(Rect(345,427,64,32), TextureButton1, "")){
                 //Pause Timer
                 clockIsPaused = !clockIsPaused;
                 audio.PlayOneShot(beep1);
                   TextureButton1 = $$anonymous$$iButtonTexture2;   //Swap to Texture2
                   
                  }
                  
                  if (GUI.Button(Rect(420,427,64,32), TextureButton2, "")){
                 //clockIsPaused = !clockIsPaused;//Reset Timer
                 audio.PlayOneShot(beep2);
                 TextureButton2 = $$anonymous$$iButtonTexture2;   //Swap to Texture2
                 
                 timeRemaining = startTime;
                 TextureButton2 = $$anonymous$$iButtonTexture;
                  }
  }
  
  function DOCountdown()
  {
      //timeRemaining = startTime -Time.time;
      timeRemaining -= Time.deltaTime;
  
      percent = timeRemaining/startTime * 100;
      if (timeRemaining < 0)
      {
         timeRemaining = 0;
         clockIsPaused = true;
         //TimeIsUp();
         Debug.Log("Time is up!");
         Application.LoadLevel ("$$anonymous$$nightSpinningLogo_No$$anonymous$$ITTintro");
         
      }
      //ShowTime();
  }
avatar image KnightRiderGuy · Dec 16, 2014 at 06:39 PM 0
Share

I got it working but having difficulty trying to figure out my GUI pause/unpause and reset buttons. I keep mucking about with it but having difficulty trying to figure out what is going on inside of the code.

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

28 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

Related Questions

Reload level after timer hits 0 1 Answer

How to make reverse countdown timer in unity? 1 Answer

Speed up timer 1 Answer

How do you make a countdown timer that will lose time if you press a button? 1 Answer

Stop and Pause Timer 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