Published on

Unity for beginner! How to switch to another scene and how to reload scene.

Authors
  • Evelyn

Please share this article if you like!

In many cases, we need to switch between scenes when creating a game.

Separating resources for each scene will also improve performance, so I would definitely like to actively switch scenes.

To be covered in this article
  • The LoadScene method of the SceneManager
  • How to reload a scene
  • What to do if scene switching becomes heavy and what to do about it

Table of Contents

Setup

In order to perform screen transitions, we need to add a scene to the build settings.

Bring up the Build Settings window, and either drag and drop the Scene file from your project, or click the Add Open Scene button to add it to the Scenes in Build section of the Build Settings.

Scene transition

The code for screen transition is as follows.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement; // need this line

public class SceneTransition: MonoBehaviour
{
    public void ChangeScene(string nextSceneName)
    {
        SceneManager.LoadScene(nextSceneName);
    }
}

Use UnityEngine's SceneManagement.

using UnityEngine.SceneManagement;

The LoadScene method of the SceneManager can be used to switch screens.

SceneManager.LoadScene(nextSceneName);

Reloading a scene

If we apply the previous description, we can also reload the screen.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement; // need this line

public class SceneReloader : MonoBehaviour
{
    public void ReloadThisScene()
    {
        SceneManager.LoadScene(SceneManager.GetActiveScene().name);
    }
}

The GetActiveScene method can be used to get the current scene.

SceneManager.GetActiveScene().name

Screen transition with loading

Screen transitions may take some time.

In such cases, loading can be displayed between screen transitions to reduce the user's stress.

The following article describes the implementation in detail, so please read it as well.

Summary of this article

What did you think?

In this article, we've provided information about scene switching, from the basic method to how to solve the problem of slow scene switching.

Scene switching is an essential technique in game development, so be sure to keep it in mind!

I would be happy to contribute to your game development in any way I can.

Thank you for reading to the end.

Please share this article if you like!