In this tutorial, we will learn the SceneManager class, and we will talk about the static methods in it, also we’ll look at the SceneManagment namespace it is in.
Topics To Learn
- What Is Scene Management Namespace?
- SceneManager Static Methods
- CreateScene Method
- GetActiveScene Method
- LoadScene Method
- GetSceneByName Method
- GetSceneByBuildIndex Method
- LoadSceneAsync Method
- MoveGameObjectToScene Method
What Is Scene Management?
Scene Management is a namespace used for all scene operations. Scene transitions etc. The SceneManagment namespace is used for all scene operations.
SceneManager Static Methods
Let’s start to learn important methods that will work for you. The methods we will learn in this section are: CreateScene, GetActiveScene, LoadScene, GetSceneByName, GetSceneByBuildIndex and MoveGameObjectToScene.
CreateScene
It is used to create a copy scene by active scene in an active scene. The method takes 1 parameter, this parameter is the name of the scene.
using UnityEngine;
// For Use SceneManager
using UnityEngine.SceneManagement;
public class Scene: MonoBehaviour
{
private void Awake()
{
// When Start Game Create New Scene
var scene = SceneManager.CreateScene("NewScene");
}
}
It can be used to make rapid changes on the stage, and you can make fast transitions such as day and night transitions with this method
GetActiveScene
The GetActiveScene method returns all information about the active scene, which you can use to store the properties of the active scene.
using UnityEngine;
using UnityEngine.SceneManagement;
public class Scene: MonoBehaviour
{
private void Awake()
{
var scene = SceneManager.GetActiveScene();
Debug.Log("Scene Name: " + scene.name);
Debug.Log("Build Index: " + scene.buildIndex);
}
}
Output: Scene Name: SampleScene Build Index: 0
LoadScene
Used to skip to the given scene. Scene name or build index value that can be used to switch to the desired scene.
using UnityEngine;
using UnityEngine.SceneManagement;
public class Scene: MonoBehaviour
{
private void Awake()
{
// Using Name
SceneManager.LoadScene("2");
// Using Build Index
SceneManager.LoadScene(1);
}
}
To add BuildIndex, file menu -> Build Settings -> Add Open Scenes, you can add your active scene to the build settings with this method.
GetSceneByName
Searches for scenes through Build Settings with the scene name you entered available to find a scene by scene name range.
using UnityEngine;
using UnityEngine.SceneManagement;
public class Scene : MonoBehaviour
{
private void Awake()
{
var x = SceneManager.GetSceneByName("SampleScene");
}
}
GetSceneByBuildIndex
It works just like GetSceneByName, except that the int (Build Index) parameter is used instead of a string parameter.
using UnityEngine;
using UnityEngine.SceneManagement;
public class Scene : MonoBehaviour
{
private void Awake()
{
var x = SceneManager.GetSceneByBuildIndex(0);
}
}
LoadSceneAsync
Scene asynchronous loading is a good alternative to LoadScene. It loads the scene by dividing it into frames in the background.
LoadScene loads the scene simultaneously, and the loading screen can be displayed during this pause, so scene transitions are faster because of LoadSceneAsync loads in the background.
using System.Collections;
using UnityEngine;
using UnityEngine.SceneManagement;
public class Scene: MonoBehaviour
{
void Update()
{
if (Input.GetKeyDown(KeyCode.Space))
{
// Use a coroutine to load the Scene in the background
StartCoroutine(LoadYourAsyncScene());
}
}
IEnumerator LoadYourAsyncScene()
{
AsyncOperation asyncLoad1 =
SceneManager.LoadSceneAsync("SampleScene");
// Wait until the asynchronous scene fully loads
while (!asyncLoad1.isDone)
{
// For Output On Console
Debug.Log("Loading...");
yield return "Loading...";
}
}
}
Output: Loading... Loading...
MoveGameObjectToScene
it is used to switch the game object from one scene to another with the same features and exactly the same.
You can only move root GameObjects from one Scene to another. So the GameObject you can move should not be a child of another GameObject.
make sure to use DontDestroyOnLoad on the GameObject you would like to move to a new Scene, otherwise, Unity deletes it when it loads a new Scene.
using System.Collections;
using UnityEngine;
using UnityEngine.SceneManagement;
public class Scene : MonoBehaviour
{
public GameObject obj;
void Awake()
{
StartCoroutine(Load());
}
IEnumerator Load()
{
Scene currentScene = SceneManager.GetActiveScene();
// Load New Scene
SceneManager.LoadScene("Scene2", LoadSceneMode.Additive);
// Move Object To New Scene
SceneManager.MoveGameObjectToScene(obj, SceneManager.GetSceneByName("Scene2"));
yield return null;
// Remove Previous Scene
SceneManager.UnloadScene(currentScene);
}
}
The object you create will switch to the other scene, other objects, lights, etc. in the previous scene. not all objects will be loaded.
You can use this method when migrating your character to a new map.

