Introduction
In this article we will learn about scene handling. But, before we learn about scene handling, it's essential to understand what Scene in Unity is.
Scenes are the objects in the game that contain a particular scenario.
Assume you have a game with a universe divided into two halves, one on Earth and the other on an alien space planet. We can say that this world can be divided into two scenes (However, this may not be the case for more giant games), One is for the Earth, and the other is for the alien planet in space.
Let's move to the next section to know more about Scene Handling.
Scene Handling in Unity
Scenes in Unity can be considered discrete levels or screens, such as a primary menu. They are game or application assets that contain all or part of the game or application.
For example:- a simple game could be built in a single scene. In contrast, a more complicated game could have one Scene per level, each with scenery, characters, obstacles, decorations, and user interface. In a project, you can make as many scenes as you like.
Unity has started using a new library to access Scenes and traverse between them in the last several versions. SceneManagement is the name of this library. We must first include this library before we can utilise it. We execute this by writing at the beginning of the script.
using UnityEngine.SceneManagement
The SceneManager is the class you will encounter and utilise the most in this collection.
We'll see steps for loading scenes in Unity in the next section.
Loading Scenes in Unity
The steps for Loading Scenes in Unity are as follows:
STEP 1:- Confirm that the Scene you want to load is included in your Build Settings. Otherwise, when you try to access the Scene via code, it won't be recognised, and you'll get an error.
STEP 2:- In your script, include the Unity Scene Management library. This can be added to a freshly constructed GameManager Script to help track the game's progress.
using UnityEngine.SceneManagement
The stages that follow will take place in the same script as Scene Management.
STEP 3:- Make a bool variable to track whether or not the game is over.
*If you're following this instruction to learn how to load scenes in general, you'll finish at step 4, and you can skip this step. You also don't need to provide the game over the condition in the input.*
Private bool _Isgameover = false;
STEP 4:- Set the user input for the game's restart key in the void Update. It can be any key. In my case, I'm using "RS” for Restart.
You have access to the SceneManager, which will load your selected Scene because you already added the scene management library.
Void update()
{
if(Input.GetKeyDown(KeyCode.RS) && _Isgameover == true)
{
SceneManager.LoadScene(0);
}
}
The Scene is identifiable by its name or the scene number assigned. The Scene is recognised in the example above by its number, which is faster than using a string/the Scene's name.
STEP 5:- To modify the value of the bool variable from step 3, create a void method.
*It's best to use methods to change bool variables rather than directly.*
public void gameover()
{
_Isgameover = true;
}
In the end, you will get an output like below
In the next section, we'll see FAQs related to the topic.