- Home /
Countdown after resume game.
Hey! I'm currently working on a mobile game and I'm trying to make a countdown timer after you unpause the game. I followed a tutorial to make it but it never worked for me. Here is my code, I would love any help/suggestions:
//method number one for time(not working)
IEnumerator cdToStart()
{
Debug.Log("TIME SHOULD START");
while (cdTime > 0)
{
Debug.Log("TIME AT " + cdTime);
cdDisplay.text = cdTime.ToString();
yield return new WaitForSeconds(1f);
cdTime--;
}
cdDisplay.text = "GO!";
Time.timeScale = 1f;
isPaused = false;
yield return new WaitForSeconds(1f);
CountDownUI.SetActive(false);
}
//method number two for time(not working)
IEnumerator Countdown(int seconds)
{
int counter = seconds;
while (counter > 0)
{
yield return new WaitForSeconds(1);
counter--;
cdDisplay.text = counter.ToString();
}
cdDisplay.text = "GO!";
yield return new WaitForSeconds(1);
Time.timeScale = 1f;
isPaused = false;
CountDownUI.SetActive(false);
}
public void resumeGame()
{
pauseMenuUI.SetActive(false);
CountDownUI.SetActive(true);
Debug.Log("YOU PRESSED IT");
//StartCoroutine(cdToStart());
StartCoroutine(Countdown(5));
}
Answer by WeirdBeardDev · Oct 11, 2020 at 08:53 PM
Since timescale=0 then you should use WaitForSecondsRealTime, which uses unscaled time.
Amazing! Its Works! Thank you so much, but when the text turns into "GO!" it isn't really in the center of the screen... Is there any chance it is possible by code?
You'll set those properties in the Inspector. $$anonymous$$ake sure the gameObject that has the text on it is positioned where you want it, and make sure you set the alignment of the text. Don't forget to mark my answer as correct.
Answer by unity_bZdtBAhk-V-2wg · Oct 11, 2020 at 07:07 PM
On line 15 you're setting Time.timeScale = 1f;
. What was Time.timeScale
before that? Just make sure Time.timeScale
is not 0f
when the non-working method is called. Instead maybe turn different scripts off to achieve the same thing
Answer by Elior_Y · Oct 11, 2020 at 07:14 PM
The time scale was 0f, maybe that why.... But like I want to pause the game, when pressing resume do a 5 second countdown and then resume the game.
Answer by Content1of_akind · Oct 11, 2020 at 09:42 PM
For countdown checkout https://answers.unity.com/questions/225213/c-countdown-timer.html And I think you want to reset the countdown to whatever number and then reactivate it with a bool being true when resumed. but as they said with Time.timescale = 0; should stop everything from continuing and Time.timscale = 1; will resume everything again.