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");
}
}
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
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");
}
In StopTimer you would just have to set pauseTimer to true, then set it to false in StartTimer.
Runalotski had mentioned add a Bool but I'm not sure I follow where or what?
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.
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");
}
}
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.
Your answer
Follow this Question
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