Unity Scene Management
Use this skill when loading, unloading, or organizing scenes, implementing additive scene architectures, or building scene transition flows in Unity.
Prevents a GameObject from being destroyed when a new scene loads (in Single mode).
public class AudioManager : MonoBehaviour
{
private static AudioManager _instance;
private void Awake()
{
// Singleton guard -- prevents duplicates when returning to a scene
if (_instance != null)
{
Destroy(gameObject);
return;
}
_instance = this;
DontDestroyOnLoad(gameObject);
}
}
Pitfalls:
Scenes must be in File -> Build Settings to be loaded by name or index.
// Check if a scene is in build settings (editor utility)
#if UNITY_EDITOR
public static bool IsSceneInBuild(string sceneName)
{
foreach (var scene in UnityEditor.EditorBuildSettings.scenes)
{
if (scene.enabled && scene.path.Contains(sceneName))
return true;
}
return false;
}
#endif
CI/automation tip: Validate that all referenced scenes are in Build Settings as part of your build validation step. Missing scenes cause runtime errors with no compile-time warning.
Unity supports having multiple scenes open simultaneously in the editor. This is useful for the bootstrapper pattern.
Hierarchy:
_Boot (loaded)
GameBootstrapper
AudioManager
_UI (loaded, additive)
Canvas
EventSystem
Level_01 (loaded, additive, active)
Terrain
Player
Enemies
Editor workflow:
_Boot scene_UI and Level_01 into Hierarchy (or File -> Open Scene Additive)Level_01 -> Set Active SceneGotcha: Objects dragged between scenes in the editor change scene ownership. This can silently break references. Always verify an object's scene in the Inspector header.
| Game Type | Recommended Pattern |
|---|---|
| Simple (1-5 scenes) | LoadSceneAsync with Single mode + loading screen |
| Medium (menu + levels) | Bootstrapper + additive loading |
| Large (open world) | Addressables + streaming + additive loading |
| Prototype / game jam | Synchronous loading is fine |