How to load to a new scene in Unity
Say you have this scene, called Scene_1:
And you would like to move to another scene called Scene_2 and vice-versa via a button:
You will need to hook the button with a method that can perform the scene change. In this case, we will use the SceneChanger.cs script which contains this public method so that it can be used from outside this class like a button. The snippet below has a method that will load a scene based on its name via
using UnityEngine.SceneManagement;public class SceneChanger : MonoBehaviour
public void LoadNewScene(string scene_name)
We will need to place this script in a GameObject. I created a GameObject called UI_Manager and added SceneChanger.cs script on it.
The next step is to call this under On Click() interaction of the Button.
Simple click +, drag the UI_Manager object and select SceneChanger.LoadNewScene as the function. Since we will be transitioning to Scene 2, the parameter should be Scene_2:
I also added the same UI_Manager and interaction on Scene 2 except that the parameter is Scene_1.
Next, Select File->Build Settings… and drag the Scenes in the Scenes In Build list to make sure both scenes can be loaded in the game.
Close the Build Settings dialog and click Play to test.