- Home /
How to restart level when timer reaches 0?
I watched a tutorial in YouTube about creating a countdown timer. It works well. Problem is, the timer still keeps going even when it reaches 0.
How do I restart the scene when the timer hits 0?
HERE'S THE TUTORIAL I WATCHED: https://www.youtube.com/watch?v=w33cOjMT0fE
Answer by GenericToast · Sep 14, 2017 at 01:35 AM
EDIT : I mixed up c++, c# and java, change the first line to import UnityEngine.SceneManagement;
I copied the script from the video, just add 2 lines of code
#include UnityEngine.SceneManagement; //include this to use the SceneManager
function Update()
{
timer -= Time.deltaTime;
if(timer <= 0)
{
timer = 0;
//You can restart the scene here
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
}
}
What SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
does is get the current scene and load it again, which simulates a 'restart' of the current scene
I added those 2 lines and it gave me this error: "Assets/Timer.js(1,2): BCE0044: expecting 'p', found 'i'."
Woops my bad. I mixed up the scripting languages, i think this is how you do it for javascript. Replace the first line with this
import UnityEngine.Scene$$anonymous$$anagement;
Thanks! It was actually
import UnityEngine.Scene$$anonymous$$anagement;
but thanks for the help! It works fine now!
Your answer
Follow this Question
Related Questions
I can't give damage with these scripts 2 Answers
How do I Start a Terrain Extension Script? 0 Answers
Disable the EventTrigger or Box Collider via C# Script 2 Answers
Inherit functions that work properly 1 Answer
Scene tree text generation 1 Answer