📖 Scenario: You are making a simple Unity game where the player can move between two scenes: a Main Menu and a Game Level. You want to load and unload these scenes properly to save memory and keep the game smooth.
🎯 Goal: Build a Unity script that loads the MainMenu scene first, then loads the GameLevel scene additively when a button is pressed, and unloads the MainMenu scene to switch smoothly between scenes.
📋 What You'll Learn
Create a script to manage scene loading and unloading
Load the MainMenu scene at start
Additively load the GameLevel scene on command
Unload the MainMenu scene after loading GameLevel
Use UnityEngine.SceneManagement namespace
💡 Why This Matters
🌍 Real World
Games often have multiple scenes like menus, levels, and settings. Loading and unloading scenes properly helps keep the game fast and uses less memory.
💼 Career
Understanding scene management is essential for Unity developers working on games or interactive applications to create smooth user experiences.
Progress0 / 4 steps
1
Setup initial scene loading
Write a Unity C# script called SceneController. Inside the Start() method, first call DontDestroyOnLoad(gameObject); to make this SceneController persist across scene changes, then load the scene named MainMenu using SceneManager.LoadScene with LoadSceneMode.Single.
Unity
Hint
Use DontDestroyOnLoad(gameObject); first to persist the controller across scenes, then SceneManager.LoadScene with the scene name "MainMenu" and LoadSceneMode.Single to load the scene alone.
2
Add a method to load the GameLevel scene additively
Add a public method called LoadGameLevel() to the SceneController class. Inside it, load the scene named GameLevel using SceneManager.LoadScene with LoadSceneMode.Additive.
Unity
Hint
Create a public method LoadGameLevel() and inside it call SceneManager.LoadScene with "GameLevel" and LoadSceneMode.Additive.
3
Add a method to unload the MainMenu scene
Add a public method called UnloadMainMenu() to the SceneController class. Inside it, unload the scene named MainMenu using SceneManager.UnloadSceneAsync.
Unity
Hint
Create a public method UnloadMainMenu() and inside it call SceneManager.UnloadSceneAsync with "MainMenu".
4
Print confirmation after loading and unloading scenes
Add Debug.Log statements inside LoadGameLevel() and UnloadMainMenu() methods to print "GameLevel scene loaded" and "MainMenu scene unloaded" respectively.
Unity
Hint
Use Debug.Log("GameLevel scene loaded") inside LoadGameLevel() and Debug.Log("MainMenu scene unloaded") inside UnloadMainMenu().
Practice
(1/5)
1. What does loading a scene in Unity do?
easy
A. It changes what the player sees by switching or adding scenes.
B. It deletes all game objects permanently.
C. It saves the current game progress automatically.
D. It pauses the game without changing the view.
Solution
Step 1: Understand scene loading purpose
Loading a scene changes the visible content by switching or adding new scenes.
Step 2: Compare other options
Deleting objects, saving progress, or pausing are unrelated to scene loading.
Final Answer:
It changes what the player sees by switching or adding scenes. -> Option A
Quick Check:
Scene loading = change visible scenes [OK]
Hint: Loading scenes changes what player sees [OK]
Common Mistakes:
Confusing loading with saving game progress
Thinking loading deletes objects
Assuming loading pauses the game
2. Which of the following is the correct syntax to load a scene additively in Unity using C#?
easy
A. SceneManager.LoadScene("Level2", LoadSceneMode.Additive);
B. SceneManager.LoadSceneAdditive("Level2");
C. SceneManager.LoadScene("Level2", true);
D. SceneManager.AddScene("Level2");
Solution
Step 1: Recall Unity scene loading syntax
The correct method is LoadScene with scene name and LoadSceneMode.Additive as parameters.
Step 2: Check each option
Only SceneManager.LoadScene("Level2", LoadSceneMode.Additive); uses the correct method and enum for additive loading.
Final Answer:
SceneManager.LoadScene("Level2", LoadSceneMode.Additive); -> Option A
A. Only the "Game" scene will be loaded, replacing "Menu".
B. Only the "Menu" scene will be loaded, ignoring the second call.
C. Both "Menu" and "Game" scenes will be loaded together.
D. An error will occur because you cannot load two scenes in a row.
Solution
Step 1: Understand default and additive loading
The first call loads "Menu" scene normally (single mode), replacing current scenes. The second call loads "Game" additively, adding it without unloading "Menu".
Step 2: Analyze the sequence
Since the second call uses additive mode, both scenes "Menu" and "Game" will be loaded together.
Final Answer:
Both "Menu" and "Game" scenes will be loaded together. -> Option C
Quick Check:
Additive loading adds scene without unloading [OK]
Hint: Additive mode loads scene without unloading others [OK]
Common Mistakes:
Assuming first load is replaced by second always
Thinking additive unloads previous scenes
Believing multiple loads cause errors
4. Identify the error in this Unity C# code for unloading a scene:
SceneManager.UnloadScene("Level1");
medium
A. UnloadScene is deprecated; use UnloadSceneAsync with scene name.
B. UnloadSceneAsync should be used instead of UnloadScene.
C. There is no error; the code unloads the scene correctly.
D. UnloadScene requires a Scene object, not a string.
Solution
Step 1: Check Unity API for unloading scenes
SceneManager.UnloadScene requires a Scene object as parameter, not a string name. No overload accepts string directly.
Step 2: Analyze the code
The code will not compile. Correct usage: Scene scene = SceneManager.GetSceneByName("Level1"); SceneManager.UnloadScene(scene);
Final Answer:
UnloadScene requires a Scene object, not a string. -> Option D
Quick Check:
UnloadScene(Scene), not (string) [OK]
Hint: UnloadScene requires Scene object, not string [OK]
Common Mistakes:
Passing string name instead of Scene object
Thinking UnloadScene is deprecated
Assuming the code runs without error
5. You want to load a new scene "Battle" without unloading the current "Town" scene, but only if "Battle" is not already loaded. Which code snippet correctly does this in Unity?
hard
A. SceneManager.UnloadSceneAsync("Town");
SceneManager.LoadScene("Battle");
B. if (!SceneManager.GetSceneByName("Battle").isLoaded) {
SceneManager.LoadScene("Battle", LoadSceneMode.Additive);
}
C. SceneManager.LoadScene("Battle", LoadSceneMode.Single);
D. SceneManager.LoadScene("Battle");
Solution
Step 1: Check if "Battle" scene is already loaded
Use SceneManager.GetSceneByName("Battle").isLoaded to verify if scene is loaded.
Step 2: Load "Battle" additively if not loaded
If not loaded, call LoadScene with LoadSceneMode.Additive to keep "Town" scene loaded.
Final Answer:
if (!SceneManager.GetSceneByName("Battle").isLoaded) {
SceneManager.LoadScene("Battle", LoadSceneMode.Additive);
} -> Option B