Transition between Scenes
Like many movies made today, there are many scenes that make up a game. Whether it’s a shot in space all the way down onto the streets, each scene can help the player and the developer to identify what’s going on. Many new developers might feel like they can shove everything into one scene, but sooner or later all the objects will clutter up to the point of not knowing what is what. On the other hand, players might not feel immersed if an object that doesn’t belong in the scene shows up.
From indie games to triple As, it’s really hard to find a game without a main menu when the player starts the game. Moreover, the main menu can help organize broad items like stages, new/load save game file, and audio/visual options. As a result, I believe there should be at least 2 scenes: the game scene and the main menu. And luckily for us, though the transition between the main menu and the scene of the game might seem complicated, in Unity, they simplified the process.
Step One: Adding the scenes
Once you created at least 2 scenes, you’ll need to add them to the Build Settings. You can access it by
File -> Build Settings or
Ctrl + Shift + B
Once you open the Build Settings, you can add them by dragging them from the Project window and into the Build Settings Window
Step 2: Creating a Script for Transition
Create a new script, or open an existing one if you’re planning to transition the scenes automatically, like after beating a stage.
Once you’re in the script you want to load the transition, head towards the top and add the following line:
This will help your code editor to determine that you want to include Unity’s SceneManagement class in this script.
Now, it’s time to create a new method in hold the code. However, this step is dependent on what you’re doing. If you’re looking to use a button to start the transition, you would want to create a public method. However, if you have other means on transition, you can keep the new method private.
After this, the next step is to add the one line of code that allows you to transition:
Note that the number in the Parentheses is based on the number located in the Build Settings. Because I added the Game scene after the Main Menu, the Game scene is 1. If, by chance, you don’t want to use the number, you can always type in the name of the scene within the parentheses.
Finally, if you’re creating the script, attach it to one of the objects on the screen. Because this is the Main Menu, I decided to attach it to the Canvas game object.
Optional Step: Adding UI Elements
Although you’re not required to add UI elements in order to transition from one scene to another it’s highly recommend when there’s no other way, like on a Main Menu. The easiest way to transition from the main menu and into the game is via a button.
Once you create a button, you want to view its properties in the Inspector window. Focus on the Button portion of the Inspector Window and towards the bottom, you’ll see a plus and minus buttons. Click on the plus button to add to the List.
The dropdown menu has multiple options, but because we want this button to only work when the game is running, we want to select Runtime only.
Right below that, you would want to select object that has the script you created or modified back in Step 2.
Lastly, use the dropdown on the right to select the script you created/modified. Once you do, select the public method you made and that’s it; you just created a button that allows you to transition from one scene to another.