Loading Scenes in Unity

You have created your main menu and now you want to be able to start the game using the button you added. To do this you are going to use On Click() and scene management.

The first thing that you will want to do is to add your scenes the the build settings. Go to file >> Build settings.

Now you can drag your scenes into the Scenes in Build window. The scenes will load from top to bottom if not specifically told which scenes to load so make sure the scenes are in order.

Take note of the numbers next to the scenes. These are the scene indexes

In the Main Menu scene add a script to your canvas. Make sure not to name it Scene Manager or Load Scene because these names may cause errors in the script.

Open the script and add Using UnityEngine.SceneManagement; to the namespace at the top.

Delete the Start and Update methods and create a new method. This method has to be public or the On Click() function will not work.

In this function add SceneManager.LoadScene(Scene Index);

You can also substitute the scene index for the name of the scene but this method will be slower than using the index.

Back in Unity click on the button to open the inspector and find the On Click() function.

Drag your canvas into the function and then using the drop down menu to find your script and then the method you just created.

Now the button will load the scene when it is pressed.



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store