Other
How to make my game stop once the timer meets zero in unity.
I'm trying to make a countdown timer, that ends the game once the timer meets zero.
public class StartGame : MonoBehaviour
{
public float timeLeft = 3.0f;
public Text startText; // used for showing countdown from 3, 2, 1
void Update()
{
timeLeft -= Time.deltaTime;
startText.text = (timeLeft).ToString("0");
if (timeLeft <= 0)
{
GameOver();
timeLeft = 0;
}
}
public void GameOver()
{
Application.Quit();
}
}
Hello there.
I dont get whats the problem. This code should work.
Well, it not stops the game, it quits the game. What do ou mean with stop? Can make all things depending on time to stop with Time.timeScale=0..
https://docs.unity3d.com/ScriptReference/Time-timeScale.html
Please, explain what does not work with your current script.
$$anonymous$$eep in $$anonymous$$d that Application.Quit()
won't work in the editor.
See https://answers.unity.com/questions/899037/applicationquit-not-working-1.html
Does that script not work or do you want to just change scenes ?
@tormentoarmagedoom @$$anonymous$$okomaki The Application.quit() was supposed to be a place holder for later, in the future, i wanted to be able to go to the previous scene once the time reached zero
Answer by tormentoarmagedoom · Oct 08, 2019 at 07:24 PM
Hello.
For your Post, your code and your comments, I understand nothing.
This code does exactly what you are asking for,
Post closed.