- Home /
How do i restart my game if countdown timer runs out on a scene
I have this script which is what i use as my timer to countdown the seconds, the timer stops at 0 but i don't know how to make the game restart when it reaches zero. Here's the script:
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using UnityEngine.SceneManagement;
public class MyTimer : MonoBehaviour { public float MyCoolTimer = 60; public Text timerText;
// Use this for initialization
void Start()
{
timerText = GetComponent<Text>();
}
// Update is called once per frame
void Update()
{
MyCoolTimer -= Time.deltaTime;
timerText.text = MyCoolTimer.ToString("f1");
print(MyCoolTimer);
if (MyCoolTimer == 0)
{
MyCoolTimer = 0;
}
}
}
Answer by Destolos · Aug 20, 2017 at 08:32 AM
I found this in another post SceneManager.LoadScene( SceneManager.GetActiveScene().name );
Answer by FerdinandBuagas · Nov 08, 2017 at 05:09 AM
if (MyCoolTimer < 0)
{
SceneManager.LoadScene (SceneManager.GetActiveScene ().buildIndex);
}
Answer by msafa · Nov 08, 2017 at 06:20 AM
(I think its not possible to catch that the time is equal to 0. Use < 0 in your if statement.) Change it your condition to Also assign the new value to MyCoolTimer in it.
if (MyCoolTimer < 0)
{
SceneManager.LoadScene (SceneManager.GetActiveScene ().buildIndex);
}
Your answer
Follow this Question
Related Questions
Making a Timer Out out of 3D Text using C#. 1 Answer
Timer going negative...Help 2 Answers
Distribute terrain in zones 3 Answers
How to restart a level with countdown? 4 Answers
Countdown timer into text c# 1 Answer