Signup/Sign In

Changing Scenes in Unity 3D

What the Scene Management library is most used for is jumping between scenes. It is one of the most simplest things to do but is vital to the creation of a game

Let us now see how it is actually done

Now in your Assets folder, create 2 new scenes and name them “Scene1” and “Scene2”.

Changing Scene images

To create a new Scene,Right-Click in the assets panel area and select Create > Scene.

Here what we aim to do, is that we should be able to skip between scenes on the press of a button (UI element). We can do this in various ways but to facilitate understanding, we will be using the simplest way. We will be creating a SceneChanger Object which will handle all the scene changing code (whatever little there is of it).

Also generally when a scene is changed from one to another, all the instances of the game objects, scripts, etc. belonging to that scene are destroyed and the ones from the new one are loaded. Another method is to use the DontDestroyOnLoad() function which will maintain an object reference across scene changes but this well be touched upon in another tutorial


SceneChanger Object Creation and Prefab

Alright, so back in Unity, in the Hierarchy tab of "Scene1", create an Empty Game Object and name it "SceneChanger"(do it yourself).

Game Object images

Make sure that its position and rotation are at <0,0,0> and the scale is at <1,1,1> in the Transform. It doesn’t affect the working but it is a good practice to keep GameObjects that possess only their transform to be placed at the origin.

Now add a script to the “SceneChanger” object and also name it “SceneChanger”. (do it yourself).

Now open the script in MonoDevelop/Visual Studio and enter the following code in it:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

public class SceneChanger : MonoBehaviour
{
	public void ChangeScene(string sceneName)
	{
		SceneManager.LoadScene (name);
	}
	public void Exit()
	{
		Application.Quit ();
	}
}

In this code, we have included the UnityEngine.SceneManagement library which allows us to use the SceneManager class.

In the ChangeScene(string name) function, we take take input a string of name sceneName. This is then passed into the SceneManager.LoadScene() function. This function loads the new scene defined by its name (if it exists).

In the Exit() function, we use the Application.Quit() function. This function has the basic task of quitting an application i.e. a similar function that the red X or the close button on the top right of the screen has

Here we have created an object. Now to create its Prefab, simply select the object in the Hierarchy and drag-drop it inside the Assets folder in the Project tab. After doing this you will see a blue cube with the same name of the object. This is the object prefab.

Scene Change images

Now save the scene and double click on “Scene2”. In this scene, simply drag and drop the SceneChanger prefab into the Viewport or Hierarchy tab. An instance of the SceneChanger will be created.


Creating the UI and Prefab

We have created a SceneChanger object in the previous section. Now we will create the UI.

In the UI we will have a text object stating the scene name and a button that will change the scene for us.

So, right click in the Hierarchy tab and select UI > Panel. Change the color of the panel to whatever you desire (try it yourself).

Again right click on the Hierarchy tab and select UI > Text. Resize it and place it anywhere you desire (try yourself).

Once more, right click in the Hierarchy tab and select UI > Button. Resize it and place it anywhere you desire (try yourself).

Your final result should look something like this

UI and Prefab

Now change the text to “THIS IS SCENE 1”. Also change the text in the button to “CHANGE SCENE” (find the text object for button as a child of the button object). The result is as follows:

Change Scene images

Now select the Button object in the Hierarchy. In the Inspector tab click on the plus icon in the Button (Script) component.

Inspecting the ui of images

We get this in the Button (Script) window.

Button Script images

Now drag and drop the SceneChanger object from the Hierarchy tab into the object reference space (highlighted in red)

Images Function

Now select the Function Drop Down (highlighted in red). From the drop down, select SceneChanger > ChangeScene(string).

Option Of button Function

Now you will see a blank space under the Function Drop Down. Type in it “Scene2”. Here what we have done is selected our SceneChanger object and accessed the ChangeScene() function. “Scene2” is the input. Now whenever the button is pressed, ChangeScene() function will be called and the scene will change to “Scene2”.

Now do the same for “Scene2” but type in “Scene1” as input and change the text in Scene2 accordingly (Do it yourself).

Now add the scenes to the Build Settings in the File menu. Doing this is imperative as without this Unity will not recognise the Scenes and no transition will take place between them.

Now when you press play. Clicking on the Button will cause the scene to change.

NOTE: We haven’t made the Exit function as in a simulation (playing scene in Unity) exit action is ignored. However, in a real build of the game we can exit using Application,Exit().