0
0
Unityframework~10 mins

Scene transitions in Unity - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Scene transitions
Start Scene
User triggers transition
Load new scene async
Wait for scene to load
Activate new scene
Unload old scene
New scene active
This flow shows how Unity loads a new scene asynchronously, waits for it to finish, activates it, and unloads the old scene.
Execution Sample
Unity
using UnityEngine;
using UnityEngine.SceneManagement;
using System.Collections;

public class SceneSwitcher : MonoBehaviour {
  public void SwitchScene(string sceneName) {
    StartCoroutine(LoadScene(sceneName));
  }

  private IEnumerator LoadScene(string sceneName) {
    AsyncOperation asyncLoad = SceneManager.LoadSceneAsync(sceneName);
    while (!asyncLoad.isDone) {
      yield return null;
    }
  }
}
This code switches scenes by loading the new scene asynchronously and waiting until it finishes loading.
Execution Table
StepActionAsyncOperation.isDoneYieldResult
1Call SwitchScene("Level2")falseStart coroutineCoroutine started
2Start LoadScene coroutinefalseCheck isDoneNot done, yield return null
3Wait framefalseCheck isDoneStill loading, yield return null
4Wait frametrueCheck isDoneLoading done, exit loop
5Coroutine endstrueNo yieldScene loaded and active
💡 AsyncOperation.isDone becomes true, ending the loading loop and activating the new scene.
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4Final
asyncLoad.isDonefalsefalsefalsetruetrue
Key Moments - 2 Insights
Why do we use a coroutine and yield return null inside the loading loop?
Because loading a scene takes time, the coroutine waits each frame until asyncLoad.isDone is true, as shown in execution_table rows 2-4.
What happens if we try to activate the new scene before loading finishes?
The scene won't be ready and may cause errors or a blank screen. The code waits for asyncLoad.isDone to be true before continuing (see execution_table row 4).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, at which step does asyncLoad.isDone become true?
AStep 4
BStep 3
CStep 2
DStep 5
💡 Hint
Check the 'AsyncOperation.isDone' column in execution_table rows.
According to variable_tracker, what is the value of asyncLoad.isDone after step 3?
Atrue
Bnull
Cfalse
Dundefined
💡 Hint
Look at the 'After Step 3' column for asyncLoad.isDone in variable_tracker.
If we remove 'yield return null' inside the while loop, what happens?
AThe scene loads instantly
BThe program freezes because the loop never yields
CThe scene never loads
DThe coroutine ends immediately
💡 Hint
Refer to the execution_table rows 2-4 where yielding allows waiting without freezing.
Concept Snapshot
Unity scene transitions use asynchronous loading.
Use SceneManager.LoadSceneAsync to load scenes without freezing.
Wait in a coroutine until loading completes (asyncLoad.isDone).
Activate the new scene after loading finishes.
Unload old scenes if needed to free memory.
Full Transcript
This visual execution shows how Unity handles scene transitions by loading a new scene asynchronously. The process starts when the user triggers a scene switch. The code calls LoadSceneAsync, which returns an AsyncOperation. A coroutine waits each frame, checking if asyncLoad.isDone is true. While loading is in progress, the coroutine yields control to avoid freezing the game. Once loading finishes, the coroutine ends, and the new scene becomes active. This method ensures smooth transitions without freezing or delays. Variables like asyncLoad.isDone change from false to true during loading, as tracked in the variable tracker. Key moments include understanding why yielding is necessary and what happens if you try to activate a scene too early. The quizzes test understanding of when loading completes and the importance of yielding.