- Home /
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();
}
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
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 :)
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.
Answer by Kiwasi · Dec 15, 2014 at 06:24 AM
For a pausable timer simply change line 54 to
timeRemaining -= Time.deltaTime;
#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();
}
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
Follow this Question
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