c# – How to load a new level in Unity 5.3+?

Question:

Prior to version 5.3, I could safely write:

Application.LoadLevel(Application.loadedLevel);

and load the level by name or index.

But now using this method does not work, writes an error:

UnityEngine.Application.LoadLevel(int) is obsolete: `Use SceneManager.LoadScene'

What kind of scene manager is this? How now to get, for example, the current scene and load it?

Answer:

Unity is developing and something is being redone, improved. Now the logical SceneManager is responsible for loading levels (scenes), and not even the whole application (Application).

The manager is now in its own namespace. Connected:

using UnityEngine.SceneManagement;

Usage example:

using UnityEngine.SceneManagement;

public class Example
{
    public void ReloadCurrentScene()
    {
        // Получаем имя текущей сцены
        string sceneName = SceneManager.GetActiveScene().name;    
        // Загружаем её саму родимую
        SceneManager.LoadScene(sceneName, LoadSceneMode.Single);
    }
}

Those. SceneManager.LoadScene now loads the scene by name or by index, SceneManager.GetActiveScene gets information about the active scene, including its name (list of scene variables: here ). Well, a small handful of different methods.

Scroll to Top