- Home /
How to restart a scene completely
I have the function which loads the scene again once a button is pushed, but the issue is when the scene is loaded, all the time variables remain from before it was re loaded, so instead of starting the player at time being 0, it re starts him at whatever his time was before the scene was loaded AND at the enemy spawn rate equal to that time.
this is my restart function: public void Restart() { SceneManager.LoadScene(SceneManager.GetActiveScene().name); }
Answer by davidcox70 · Aug 05, 2020 at 04:37 PM
Re-loading a scene will clear all variables, other than those that are preserved with dontDestroyOnLoad. However, the time function counts from the moment the game starts running, not when a scene is loaded.
To have a time that begins at scene load, declare a variable such as
Float localStartTime;
The in your Start() routine, add:
localStartTime=Time.time();
Now whenever you want to know the time your scene has been running, just use Time.time()-localStartTime;
Could I please see the full script? I'm having a similar issue and followed the answer, but nothing is happening
Your answer
Follow this Question
Related Questions
Option to restart the game? 3 Answers
Unity Editor: Button to restart unity. 1 Answer
Unity Serializer - saves delete after restarting game 2 Answers
How do I restart games with more than one level 1 Answer
How do I remove the automatic restart function from the Survival Shooter tutorial series?, 1 Answer