0
0
Unityframework~5 mins

Scene transitions in Unity

Choose your learning style9 modes available
Introduction

Scene transitions help you move smoothly from one part of your game to another. They make the game feel connected and easy to follow.

When the player finishes a level and moves to the next one.
When switching from the main menu to the gameplay screen.
When loading a new area or environment in the game.
When showing a game over or victory screen after the game ends.
Syntax
Unity
using UnityEngine.SceneManagement;

// To load a new scene by name
SceneManager.LoadScene("SceneName");

// To load a new scene by build index
SceneManager.LoadScene(1);

You need to add scenes to the Build Settings in Unity for this to work.

Scene names are case-sensitive strings matching your scene file names.

Examples
This loads the scene named "Level1" immediately.
Unity
SceneManager.LoadScene("Level1");
This loads the scene with build index 2 from your Build Settings.
Unity
SceneManager.LoadScene(2);
This function loads the "MainMenu" scene when called.
Unity
using UnityEngine.SceneManagement;

public void GoToMainMenu() {
    SceneManager.LoadScene("MainMenu");
}
Sample Program

This script shows two ways to change scenes: by moving to the next scene in order, or by loading a scene named "MainMenu".

Unity
using UnityEngine;
using UnityEngine.SceneManagement;

public class SceneTransitionExample : MonoBehaviour
{
    // Call this method to change scenes
    public void LoadNextScene()
    {
        // Get current scene index
        int currentSceneIndex = SceneManager.GetActiveScene().buildIndex;
        // Load next scene by increasing index
        SceneManager.LoadScene(currentSceneIndex + 1);
    }

    // Example: Load scene by name
    public void LoadMainMenu()
    {
        SceneManager.LoadScene("MainMenu");
    }
}
OutputSuccess
Important Notes

Make sure all scenes you want to load are added in Unity's Build Settings under 'Scenes In Build'.

Scene loading is immediate by default, so the screen will switch quickly without delay.

You can add animations or loading screens between scenes for smoother transitions.

Summary

Scene transitions let you move between different parts of your game.

Use SceneManager.LoadScene() with a scene name or build index to change scenes.

Always add your scenes to Build Settings to make them loadable.