- Scenes have to be added to the final build in the build settings. The "why" is explained below.
- If you are going to switch/load scenes in a C# script, you need to include "using UnityEngine.SceneManagement" at the top.
- SceneManager.LoadScene() - This is the command that loads a scene. You can reload the scene you are in, or switch to a different scene.
- When you switch scenes, every object in the current scene is destroyed and every object in the new scene is created/instantiated.
- If you want to carry data over to a new scene, there are two basic ways to do it:
- (1) Tell a GameObject to not get destroyed at the end of the level, so it still exists when the next level is loaded.
- (2) Write information to a file, then read information from that file later. This is a much larger topic as you should encrypt this information, and how you set a filepath depends on the platform your game is built for, so this will be covered in a later topic if we dive into it at all.
Getting Started - Add Scenes to the Build Settings
Before you can switch/load scenes, you need to add the scenes to your build in the build settings. The reasoning is that you can have many assets and scenes that you use during development that you do not want included in your final build. The final build only includes the necessary scenes, and the assets used in those scenes. The rest was just there to help you as a developer, but is not part of what the customer/player gets. Your project might be 1GB, but the final build might only be 17MB. That is an extreme example, but I hope it gets the point across.
1. From the Top Menu, select "File --> Build Settings ..."
2. Drag the scenes from the Project Window into the "Scenes in Build" area. Then use the checkboxes to indicate which scenes you want included in your build.
When this GameObject collides with anything, load the scene named "Level2".
Being a little more flexible, here you have two public variables:
- String tagName - This lets you be more flexible. For example, if the tag is "enemy" you die and reload the current scene, but if the tag is "goal" then load the next scene.
- String sceneName - This lets you use the same script on different levels, as the next scene after "Level1" is "Level2", but after "Level2" is "Level3".
Reload the current scene. You can use the method GetActiveScene() to get the current scene you are in. Then use the name attribute to reload the current scene.
Persistent Game Data
Telling a GameObject Not to Destroy Itself from Scene to Scene
One method of retaining data throughout your game is to keep a single persistent GameObject intact from scene to scene. In this way, it can hold any information you want it to hold such as: current score, number of lives remaining, items collected, etc. In the long run, and in larger projects, it is better to write encrypted data to a file. This method of a persistent GameObject is a smaller fix that brings information from one scene to another, but the information will be lost once the game is exited.
The code above does two things:
- Tells the current GameObject to never be destroyed as you switch from scene to scene. You can still destroy it using the command Destroy(), but it will not automatically be destroyed when you use SceneManager.LoadScene().
- It checks if there is a duplicate, and if so it destroys itself. Without this in place, a new duplicate persistent GameObject would be created each time you load the first scene. The next time you reload scene #1 you would have three duplicate persistent GameObjects, and so on.
Check out the YouTube videos below on how to fade, or make your own custom transformation, between scenes. The basic idea is that you record a simple animation of a sprite that covers the camera/entire-scene. It can fade from clear to solid color, or move in from the sides like a curtain, or whatever you want.
YouTube Videos on Scene Transitions
"How to Fade Between Scenes in Unity" (13min, May 2018) by Brackeys. This video shows how to record a simple animation where you fade a solid color image from transparent to solid black to fade out of a scene, and do the same animation in reverse when a new level loads.
"How to Make Cool Scene Transitions in Unity - Easy Tutorial" (9min, May 2018) by BlackThornProd. Very similar to the above video about fading in an out, but instead you record an animation of a circular sprite enclosing the screen to black. Upon opening the next scene the animation plays in reverse to open up to a new world.