Restarting a scene

Hi.

I have two scenes, main menu and the game scene.

The main menu has a ‘new game’ button which loads the game scene.

So the game opens up, you press new game and you play the game until the game ends (game over), then the menu screen gets loaded. The problem i have is that NOW, when you press the new game button, the game gets loaded from the game over section, rather than the entire scene being restarted.

I have tried

SceneManager.LoadScene(“Game”);
SceneManager.LoadScene(SceneManager.GetActiveScene().name);

And all the other ‘restarting’ scene google searches.

How do i actually restart a scene, instead of just loading it, like all the threads show?

I want the scene to reload to its start position, that is, the scene when it is first loaded, having the same result as destroying the scene and starting the scene again.

Cheers.

What you’re doing should do what you’re asking for. I can think of two possible reasons for why it’s jumping to game over:

  • you’re storing whatever information triggers the game over state in static variables, so the game over is immediately triggered
  • your game over things is on a DontDestroyOnLoad object.
SceneManager.LoadSceneAsync(SceneManager.GetActiveScene().buildIndex);

Using this since Unity 5.3

Yes they are static variables as they need to be changed and referred to by other scripts, is there a workaround?

¨There’s three ways off the top of my head:
1: reset all the static variables when you restart. This is a bit complicated if they’re all over the place.
2: Move all the static variables to a singleton object that’s still globally accessible. Then you can just dump the entire singleton (instance = new Singleton()) when you restart.
3: Stop using static variables in that way.