CHAPTER 12
Beginner
Scene Management and Game Flow
Updated: May 16, 2026
25 min read
# CHAPTER 12
Scene Management and Game Flow
1. Introduction
A video game is rarely just one continuous 3D space. You start at a Main Menu. You click "Play" and load Level 1. If you die, a "Game Over" screen appears, and you are teleported back to the beginning. This transition of states is known as Game Flow. In Unity, individual levels or menus are saved as Scenes (.unity files). In this chapter, we will master Scene Management. We will learn how to transition between levels using C#, configure the Build Settings, build a functional Main Menu, and implement a robust Pause system.
2. Learning Objectives
By the end of this chapter, you will be able to:- Understand what a Unity Scene is and how to manage multiple scenes.
- Add scenes to the Build Settings index.
-
Use
SceneManager.LoadScene()to transition between levels.
- Create a functional Main Menu with "Play" and "Quit" buttons.
-
Implement a Pause system using
Time.timeScale.
3. Scenes and Build Settings
A Scene contains all the GameObjects, lights, and UI for a specific part of your game.-
You might have a
MainMenuscene, aLevel01scene, and aLevel02scene.
- Before Unity can switch between them, it needs a master list.
- 1. Go to File -> Build Settings.
- 2. Open your Project window and drag your scene files into the Scenes In Build box.
-
3.
The order matters! The scene at index
0(the top of the list) is the one that loads automatically when the player launches the actual.exegame.
4. Loading Scenes with C#
To load a scene, your script needs the specific namespace:using UnityEngine.SceneManagement;.
csharp
5. Building a Main Menu
-
1.
Create a new Scene (Ctrl+N) and save it as
MainMenu.
- 2. Add a UI Canvas and two Buttons: "Start Game" and "Quit".
-
3.
Create a
MainMenuManagerscript.
-
4.
Hook the "Start Game" button to a function that calls
SceneManager.LoadScene("Level_01").
-
5.
Hook the "Quit" button to a function that calls
Application.Quit(). *(Note: Quit only works in the built.exegame, it does nothing when playing inside the Unity Editor).*
6. Pausing the Game (TimeScale)
How do you freeze all enemies, physics, and animations instantly to show a Pause Menu? Unity uses a global multiplier calledTime.timeScale.
-
Time.timeScale = 1f;(Normal speed).
-
Time.timeScale = 0.5f;(Matrix-style slow motion).
-
Time.timeScale = 0f;(Completely frozen).
csharp
7. Visual Learning: Game Flow Architecture
txt
8. Best Practices
-
DontDestroyOnLoad: Normally, when you load a new scene, Unity deletes absolutely everything from the old scene. If you have an AudioSource playing background music, the music will awkwardly restart. You can attach a simple script to your MusicManager containing
DontDestroyOnLoad(gameObject);. This magical function tells Unity to carry the object over into the new scene without destroying it, ensuring uninterrupted music!
9. Common Mistakes
-
Restarting a Paused Game: A player dies while the game is paused (TimeScale = 0), and they click "Restart Level". The level reloads, but nothing moves! The game is completely frozen!
TimeScaleis a global setting that persists across scenes. If you set it to0, you MUST remember to setTime.timeScale = 1f;inside theStart()method of your LevelManager so the game always unfreezes on a fresh load.
10. Mini Project: The Portal
Objective: Create a physical trigger that loads the next level.-
1.
Ensure you have
Level01andLevel02added to your Build Settings.
-
2.
In
Level_01, create a glowing Cylinder and check Is Trigger on its collider.
- 3. Attach this script to the Cylinder:
csharp
- 4. Walk your player into the cylinder and watch the scene seamlessly transition!
11. Practice Exercises
- 1. What menu window must you open to add your scenes into a master index list before they can be loaded via script?
-
2.
What C# variable controls the global speed of physics, animations, and
Time.deltaTime, allowing you to pause the game?
12. MCQs with Answers
Question 1
To use the SceneManager class in your C# script, which line of code MUST be present at the very top of the file?
Question 2
When the player clicks the "Quit" button on the Main Menu, the script executes Application.Quit(). Why does nothing happen when you test this in the Unity Editor?
13. Interview Questions
-
Q: Walk me through the exact pipeline required to transition from a
MainMenuscene to aLevel_01scene when a UI button is clicked.
-
Q: Explain how
Time.timeScaleworks in Unity. Provide an example of a severe game-breaking bug that can occur iftimeScaleis not managed properly across scene transitions.
-
Q: What is the purpose of the
DontDestroyOnLoadmethod? Provide a scenario where its usage is critical for a smooth player experience.
14. FAQs
Q: My game has a giant open world. Do I have to load it all at once? A: No! Advanced games use "Additive Scene Loading". You can load the "Forest" scene on top of the "City" scene seamlessly in the background without a loading screen while the player is walking!15. Summary
In Chapter 12, we connected our fragmented scenes into a cohesive game flow. We learned how to register our scenes in the Build Settings index. We utilizedSceneManager.LoadScene() to trigger level transitions physically (via Portals) and via UI (Main Menus). We programmed a robust Pause architecture by manipulating Time.timeScale to freeze the simulation, and learned the critical rule of resetting that time scale on a level reload.