Reset a Game Scene in Unity

by Lance Gold

Game reset, player levels, additional scenes add complexity and player time to a game

Return to index

The Short Version

There is one scene reloading itself as a game reset:


using UnityEngine.SceneManagement;
...
public class UIManager : MonoBehaviour
{
    [SerializeField]
    private GameObject _pressR;
    private bool _restartActive;
...
    void Start()
    {
        _restartActive = false;
...
    }

    void Update()
    {
        if (Input.GetKeyDown(KeyCode.R) &&  _restartActive)
        {
            Debug.Log("pressed 'r' with _restartActive");

            _pressR.SetActive(false);
            _restartActive = false;
            SceneManager.LoadScene("Game");
        }
...
    }
...
}

The Longer Solution Using a Class “Game_Manager”

From the online tutorial:

User input does not go inside a UI manager.

A gameManager can handle status such as:

“Is the game over?” Let’s go ahead and restart.

“Is the player still alive?”

“How many enemies are there?”

Create an empty object in the Hierarchy “Game_Manager.”

New empty Game_Manager

New empty Game_Manager

Create a new script “GameManager.” Drag and drop into the Hierarchy object.

New script GameManager

New script GameManager

In the Inspector the Game_Manager object now has the GameManager component script:

Game_Manager with script component

Game_Manager with script component

From the Unity ScriptingAPI: SceneManager.LoadScene:

SceneManager.LoadScene

Declaration

public static void LoadScene(int sceneBuildIndex, SceneManagement.LoadSceneMode mode = LoadSceneMode.Single);

Declaration

public static void LoadScene(string sceneName, SceneManagement.LoadSceneMode mode = LoadSceneMode.Single);

Description

Loads the Scene by its name or index in Build Settings.

Now the code looks as follows. GameManager.GameOver() is ready to be called by some outside class.


using UnityEngine.SceneManagement;

public class GameManager : MonoBehaviour
{
    [SerializeField]
    private bool _isGameOver;   // starts out false
    void Update()
    {
        if( Input.GetKeyDown(KeyCode.R) && _isGameOver )
        {
            SceneManager.LoadScene("Game");
        }
    }
    public void GameOver()
    {
        _isGameOver = true;
    }
}

“Game” is the name of the scene, and as more scenes are added, they appear in the project Assets — Scenes folder.

“Game” in Project Assets Scenes folder

“Game” in Project Assets Scenes folder

“Game” or any other scene added, appear in the Hierarchy with it’s game objects.

Game scene in Hierarchy

Game scene in Hierarchy

Build Settings

Check to verify scene and “Game” in the Unity Build settings.

Build Settings in Unity File menu

Build Settings in Unity File menu

The Build Settings panel may be blank. In the Unity editor, the open Scene is “Game” (this example with the player, laser fire, score top right, remaining lives top left, and enemy ships dropping down).

Build Settings with empty “Scenes to Build”

Build Settings with empty “Scenes to Build”

Click “Add Open Scenes” and “Game” appears top left with its ID number “0” top right.

Build Settings with scene “Game” and ID “0” added

Build Settings with scene “Game” and ID “0” added

The “0” Scene ID allows use of the faster syntax for LoadScene():


        if( Input.GetKeyDown(KeyCode.R) && _isGameOver )
        {
            SceneManager.LoadScene(0);  // ID 0 is scene "Game"
        }

string “Game” or int 0, not string “0”

If using the ID instead of the name, be helpful and add the comment with the scene’s name: for example “Game”.

X exit out of Build Settings.

Close Build Settings

Close Build Settings

Calling an Outside Method

Either (a) C# with No Drag and Drop

A quick review of the steps to call GameManager.GameOver() followed by a check to verify the valid assignment.


public class UIManager : MonoBehaviour
{
    private GameManager _gameManager;
...
    void Start()
    {
        _gameManager = GameObject.Find("Game_Manager").GetComponent<GameManager&rt;();

        if( _gameManager == null )
        {
            Debug.LogError("GameManager is NULL.");
        }
...
    }
...
    void GameOverSequence()
    {
...
        _gameManager.GameOver(); 
    }
}

Or (b) Drag and Drop Method using the Inspector

Add [SerializeField] to access variable in the Inspector.


public class UIManager : MonoBehaviour
{
    [SerializeField]
    private GameManager _gameManager;
...
}

Drag gameObject Game_Manager to Canvas — UI Manager (Script) — Game Manager: “None (Game Manager)”.

Save.

Drag and Drop variable contents

Drag and Drop variable contents

The Game_Manager component is now included in the UIManager. (after Save):

Component assigned in Inspector

Component assigned in Inspector

Take a look at the code without “ _gameManager = GameObject.Find… ”:


public class UIManager : MonoBehaviour
{
    [SerializeField]
    private GameManager _gameManager;
...
    void GameOverSequence()
    {
...
        _gameManager.GameOver();
    }
}