Understanding SceneManager.sceneLoaded in Unity

Understanding SceneManager.sceneLoaded and sceneUnloaded in Unity

Unity provides a global static class called SceneManager that handles scene-related functionality. Among its features, the sceneLoaded event is automatically triggered when a new scene is loaded.

Because it’s an event, you can easily register a method using +=, and Unity will call your method whenever the event fires.

When should you use it?

SceneManager.sceneLoaded is useful in situations like:

  • Resetting or initializing data after a scene is loaded
  • Having a singleton manager locate and prepare scene-specific objects
  • Running common logic automatically whenever a scene changes

When is the event triggered?

The sceneLoaded event is called after all Awake() and OnEnable() methods have finished, but before Start() is executed.

This means all scene objects are fully instantiated and active, so it’s safe to use GameObject.Find() or perform scene-level initialization.

What about when a scene is unloaded?

Alongside sceneLoaded, Unity also provides SceneManager.sceneUnloaded, which is triggered when a scene is completely removed from memory.

When using LoadScene() or LoadSceneAsync() to switch scenes, the sceneUnloaded event will be triggered once the previous scene is fully unloaded.


SceneManager.sceneUnloaded += OnSceneUnloaded;

void OnSceneUnloaded(Scene scene)
{
    Debug.Log($"Scene unloaded: {scene.name}");
}

This event is useful for releasing resources, resetting state, logging, or any other operations that should run when a scene ends.

Note that by the time sceneUnloaded is called, all objects in the scene are already destroyed. You should avoid referencing any scene-local objects at this point.

However, it is perfectly safe to perform cleanup on singleton managers or external objects from this event.

Summary

If your game structure requires resetting data or interacting with scene-specific objects after a scene transition, SceneManager.sceneLoaded is a safe and effective solution.

Conversely, if you need to handle cleanup logic when a scene ends, you should use sceneUnloaded instead.

If you're experiencing issues where Awake() or Start() are not being called after a scene reload, switching to an event-based system like this can provide more control and reliability.

Popular posts from this blog

Understanding Arrays as Reference Types in C#

Setting Up a Basic Follow Camera with Cinemachine 3.x

Understanding and Using ? (nullable) in C#