When making your games in Unity you will at some point need to load a different game scene. All games should have a main menu and this will then lead to another scene which will contain the first level or the whole game.
I now have a main menu in my game and I need to allow the player to start the game whenever they press the Play button.
There are a couple of things that you need to do to be able to handle scene changes in Unity.
First we need to add the ability to access Unity’s scene management methods. At the top of the script that is handling the button clicks I have added this line.
This now allows us the access we need to load a new scene in Unity.
The method above is run when the play button is clicked. First we access the SceneManager and the LoadScene method. The first parameter that we pass into the method is the scene that we want to load. You can either pass in a string like I have or you can pass an integer. The integer needs to correspond to the scene int that is in the game build settings.
You can see highlighted in red above the integers that correspond to the int that you would need to pass into the method instead of the string.
I have added in another parameter that means that we are only allowing one scene to be in the hierarchy at one time.
You can also load multiple scenes into the hierarchy using additive instead of single. As an example, you could use this to load scenes into the game when the player has reached a certain location. This lowers the number of processes needed for the game to run and when the player is out of view/distance of a scene it can be removed.
That’s it for this article. Scene loading can be very easy to implement in Unity.