Loading Unity’s Scenes via SceneManager.LoadScene.

If you are working on a larger game or something with multiple levels, you will need to know how to load scenes in unity. First let’s talk about what makes up a scene. A scene is where you work with content in Unity. A simple game might only have one scene with the same type of environment, whereas a more complex game might use one scene per level, or even multiple scenes in a level. The more complex games might change the environment, characters, obstacles and UI in each scene. Now the big question is how do you load them?
One wat is to use Unities SceneManager to facilitate the loading of the different scenes in your project. We will go over the SceneManager.LoadScene first, then touch on the async command.


As you can see from the above code you can load a scene with the use of a string name or the build index in your project. A build index is located in your project build settings, the index numbers are to the right of the scene name.

To load a scene you will need to have the Using statement followed by the class name SceneManager as the examlpe below:

You will then need to call LoadScene with the proper index or scene name.
Unity — Scripting API: SceneManagement.SceneManager.LoadScene (unity3d.com)



The scene name is NOT case sensitive but proper spelling is a must. The scene needs to be active in the build settings.
There are scene modes you can specify, and they are Single and Additive. Single is the most widely used for beginners and are the default without a selection made. Single will close the current scene and load the new one. Additive will load the scene to the current loaded scene or scenes, does not close out the one running.
Unity — Scripting API: LoadSceneMode (unity3d.com)
LoadScene is really not the go to for loading scenes, due to the issue of pauses or performance. The most often used should be LoadSceneAsync. This command will load the scene asynchronously in the background. For more on this see the link below:
Unity — Scripting API: SceneManagement.SceneManager.LoadSceneAsync (unity3d.com)