Getting a pause button to work on a timmer
I am trying to code a timer into my unity project and have got the timer and reset button working how I need however, I can not figure out how to code the pause button. Pleas could someone help me with this? The code that I am using is:
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using UnityEngine.SceneManagement;
public class ContdownScript : MonoBehaviour { [SerializeField] private Text uiText; [SerializeField] private float mainTimer;
private float timer;
private bool canCount = true;
private bool doOnce = false;
void Start()
{
timer = mainTimer;
}
void Update()
{
if (timer >= 0.0f && canCount)
{
timer -= Time.deltaTime;
uiText.text = timer.ToString("F");
}
else if (timer <= 0.0f && !doOnce)
{
canCount = false;
doOnce = true;
uiText.text = "0.00";
timer = 0.0f;
SceneManager.LoadScene("breathing_exercise");
}
}
public void ResetBtn()
{
timer = mainTimer;
canCount = true;
doOnce = false;
}
}
Your answer
Follow this Question
Related Questions
Problem setting Time.timeScale to 1 after being set to 0 0 Answers
Why Pause and Sound On-Off Working on pc NOT working on Android 0 Answers
How to make spawned in game objects pause with mouse click. 1 Answer
How to stop Background music from resetting when i resume game 0 Answers
Play Audio with Timers 0 Answers