- Home /
How to pause countdown
Hi Guys, I want to pause my countdown whenever I press P to pause and not pause the whole game. Please give me suggestion on my script.
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using System.Threading; using UnityEngine.Events; using UnityEngine.SceneManagement;
public class Countdown : MonoBehaviour { public GameObject Pausemenu, GoButton, MissionMenu; public GameObject PlayerPanel, MissionMenu2;
private float gameTimer;
private static bool gamePaused = true;
private float startingTime;
public float totalTime;
public Text timeLeft;
public Text text;
private float minutes;
private float seconds;
public GameObject GameOver;
[Header("--Event Timer--")]
public bool useEventTimer;
public UnityEvent TimerEvent;
void Start()
{
//startingTime = totalTime;
MissionMenu.SetActive(true);
gamePaused = true;
}
void Update()
{
if (!gamePaused)
{
gameTimer -= Time.deltaTime;
}
if (Input.GetKeyDown(KeyCode.P))
{
Pausemenu.SetActive(true);
//Time.timeScale = 0f;
gamePaused = true;
}
totalTime -= Time.deltaTime;
minutes = (int)(totalTime / 60);
seconds = (int)(totalTime % 60);
if (useEventTimer)
{
if(minutes <= 0 && seconds <= 0)
{
//totalTime = startingTime;
Debug.Log("Timer Event");
TimerEvent.Invoke();
GameOver.SetActive(true);
gamePaused = true;
//Time.timeScale = 1;
//GameIsPaused = true;
}
}
text.text = minutes.ToString() + ":" + seconds.ToString();
}
public void Go()
{
PlayerPanel.SetActive(true);
MissionMenu.SetActive(false);
//Time.timeScale = 1f;
gamePaused = false;
}
public void Resume()
{
Pausemenu.SetActive(false);
//Time.timeScale = 1f;
gamePaused = false;
}
public void restartGame()
{
SceneManager.LoadScene("Level 1");
}
public void exitGame()
{
Debug.Log("Quit");
SceneManager.LoadScene("Menu Vr");
}
public void missionMenu()
{
MissionMenu2.SetActive(true);
//Time.timeScale = 0;
gamePaused = true;
}
}
Do you did this code? seem to have implemented the feature but you dont use it
change totalTime to gameTimer on the calculus of remaining time
$$anonymous$$utes = (int)(gameTimer / 60);
seconds = (int)(gameTimer % 60);
I change it but it's not work, the countdown show 0:00. I want whenever I press P it will show pause menu but only countdown pause not the whole game
yes, seem like gameTimer never is initialized and always will have this default value...
on Start method add the initialization to gameTimer
gameTimer = totalTime;
Pd: I convert your answer to comment, because is referred as a reply to my previous comment
I do as you say but the countdown still not working. It just show 5:0
I dont understand why... Excuse the obvious, but did you put the intialization on Start method?
void Start()
{
//startingTime = totalTime;
$$anonymous$$ission$$anonymous$$enu.SetActive(true);
gamePaused = true;
gameTimer = totalTime;
}
Yes , I put gameTimer = totalTime; at start method
so the lifecicle of gameTimer shoud be
initialized on
Startmethodon pause until
gamePausedbecomes false (so after callGOorResumemethod)discounted each frame during game
raise de
TimerEventfrom their dependent variables seconds and $$anonymous$$utes
to be honest I dont understand why fail
Your answer