LevelManager in Unity 5.5

I’ve seen some posts about this topic in other categories, but as I have not seen it here for the “Making a splash screen” video, I thought it would be a good idea to post my working code here for other students.

Today when I implemented the level manager in my Glitch Garden project, I recognized unity 5.5 marks the Application.LoadLevel and the corresponding variables e.g. “loadedLevel” as deprecated.

I’ve searched then the docs of unity and found the SceneManager which serves the same purpose and seems to be the replacement for the previously mentioned methods.

To get it working, don’t forget to add the using UnityEngine.SceneManagement statement at the beginning of the script. This makes the SceneManager available in your class.

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

public class LevelManager : MonoBehaviour {

    public float autoLoadNextLevelAfter;

    void Start()
        Invoke("LoadNextLevel", autoLoadNextLevelAfter);

	public void LoadLevel(string name)
        Debug.Log("New Level load: " + name);

    public void QuitRequest()
        Debug.Log("Quit requested");

    public void LoadNextLevel()
        //Application.LoadLevel(Application.loadedLevel + 1);
        SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1);

Long Addendum: Since we’re talking about the “new” SceneManagement system in Unity 5.5, I thought I’d chip in my 2 cents on how to get behaviour similar to “OnLevelWasLoaded”.

To get behaviour similar to “OnLevelWasLoaded”, you need to use “SceneManager.sceneLoaded”, and add a “Delegate Method.”

I won’t go into detail (sadly, I don’t fully understand it myself) BUT, you can use the following code to get a method to run every time a Scene is loaded:

using UnityEngine;
using UnityEngine.SceneManagement;

// ... Other stuff here.

// FIRST: Declare that SceneManager.sceneLoaded has a method. 
// This looks VERY unusual, but all you're doing is adding a method 
// to this "event" that happens.
// i.e. "LevelLoaded" is a method defined below. This method needs to take a "Scene" 
// and a "LoadSceneMode" (single or additive) as arguments.

// AGAIN: We are "adding" a method that we want to run every time a Scene is loaded.
// This is best done when the script is enabled, as there may be times when you 
// disable it and then re-enable it again (maybe not too often, but good practice I 
// think?).
void OnEnable(){
	SceneManager.sceneLoaded += LevelLoaded; // LevelWasLoadedAndI_LikeMeatballs

// Now we have to be careful to make sure we "remove" the method from 
// SceneManager.sceneLoaded if it's no longer wanted/needed because... reasons. I 
// think this frees resources so the game/whole computer doesn't slow down. For this 
// reason, I like to go overkill and use the "removal code" whenever the 
// Script/GameObject is disabled OR destroyed. This way, I can be sure it's "gone".
void OnDisable(){
	SceneManager.sceneLoaded -= LevelLoaded; // LevelWasLoadedAndI_LikeMeatballs
void OnDestroy(){
	SceneManager.sceneLoaded -= LevelLoaded; // LevelWasLoadedAndI_LikeMeatballs

// NOW, this method will function like "OnLevelWasLoaded". You can do all the things
// you would do there, here.

// If you don't much care for "LevelLoaded" as the name of the method, you can change
// the name to something else, as long as it's also changed with 
// SceneManager.sceneLoaded.
// i.e. You could change the method to something completely ridiculous, like 
// "void LevelWasLoadedAndI_LikeMeatballs(Scene scene, LoadSceneMode mode){ ... }", 
// as long as you also change the lines:
// SceneManager.sceneLoaded += LevelLoaded; AND
// SceneManager.sceneLoaded -= LevelLoaded; TO

// SceneManager.sceneLoaded += LevelWasLoadedAndI_LikeMeatballs; AND
// SceneManager.sceneLoaded -= LevelWasLoadedAndI_LikeMeatballs;

void LevelLoaded(Scene scene, LoadSceneMode mode){
    // Here is where you put all the code you want to execute when a new level is 
    // loaded. A good example is back in Laser Defender, when we want to change our
    // background music.

I hope this helps somebody; It’ll probably be explained later in the course, but still.
I also hope it’s easy/possible to understand…


Privacy & Terms