Unity Scene Switching


Unity Scene Switching

Hello friends, in this article I will show you how you can switch between scenes in Unity. As you know, games consist of more than one scene rather than a single scene. (Start scene, intermediate scenes, game scene, ...)

There are two methods for switching between scenes, one of which is the old and the other the new method.

Before starting on both methods, first of all we need to introduce the scenes to the game, so the first thing we need to do is;
You need to go to "File > Build Settings".


After completing this process, you need to add the current scene.


You can add the current scene by clicking the "Add Open Scenes" button. After adding our scenes this way, let's move on to how to navigate between scenes.

1st Method - Application.LoadLevel(X);

With the old method Application.LoadLevel(SceneNumber & SceneName) you can set the scene you want to go to and make the switch.
To use this method, you can put this code in a function, call that function when a button is clicked, and change the scene.

Switching by Scene Number


Application.LoadLevel(1); // The "1" here is the number of the scene and it is the number that appears next to the scene we added above.

Switching by Scene Name

Application.LoadLevel("Scene1"); // The "Scene1" here is the scene name and you need to use whatever name you give to that scene.

2nd Method - SceneManager

Unity is a constantly developing and changing game engine. Although the main system doesn’t change much, some parts can change from time to time and now the first method has become deprecated, that is, you can use it if you want but it is recommended to use the second method that I will explain now.

To use SceneManager we first need to add its library to the game script file.

using UnityEngine.SceneManagement; // Add this at the top of our script file.

Now, the only thing we need to do to change the scene is the following.

  SceneManager.LoadScene(1, LoadSceneMode.Single); 

or

  SceneManager.LoadScene(1, LoadSceneMode.Additive);

by using this you can change scenes. Now, let's talk about the difference between the two.
When you change the scene with Single, all open scenes are closed and only the scene you told to open remains, but with Additive, the other scenes are not closed and continue to run in the background.
If you want to change by scene name, you can write the scene name in quotation marks instead of 1, and change to your scene according to its name, for example "Scene1".