Loading Scenes in Unity

Tristen Hart
2 min readAug 4, 2023

--

In an in-game city, every house could be a separately loaded game scene

Every video game ever made is built on scenes, which serve as the playing field for small games, and as games become bigger in scale, serve as levels or areas that need to get loaded into.

Adding a Reset Button

My space shooter games is a small project that consists of only one scene. Once the player runs out of lives and the game is over, nothing can be done unless I reset the scene through unity. What my goal is now is to have the player be able to reset the scene after a game over. For this, I need to make a Game Manager.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
//add scene manager as shown
using UnityEngine.SceneManagement;

The Game Manager script consists of a Boolean which checks if the game is over, which I want to be used for a check that allows the game to be restarted, as well as a function that switches the bool to true when called.

With SceneManagement added to the script, I am able to use SceneManager to load the game scene over again as long as “R” is pressed and the game over variable is true. _isGameOver is marked true by using GetComponent in the UIManager script once the player runs out of lives.

    private void Update()
{
if (Input.GetKeyDown(KeyCode.R) && _isGameOver == true)
{
SceneManager.LoadScene(0); //current game scene
}
}

Enable to load this scene, it first needs to be saved in the Build Settings. Go under “File> Build Settings…” and press “Add Open Scenes”. With this saved, the current scene (“game”) is saved and numbered 0. This is what LoadScene needs to use enable to understand what scene to load.

--

--