Upcoming change to unity regarding on level was loaded

Changes coming to unity regarding OnLevelWasLoaded()

this example is for 5.4 and onwards, but this may work in late versions of 5.3
i checked previous to posting this if someone else has provided this information but i didn’t find anything here so i wanted to post this tutorial

i recently was trying to help somone on the udemy course Q&A section regarding a error message that comes up when we use the OnLevelWasLoaded() which mentions that the function is marked for deletion in futre versions. This user wanted to know how to fix this for future reference, i tried to guide as best as i could as this is something that i had come across and got working on one of my projects.

i wanted to start this thread to double check if my way of going about it is right. so i ask if everyone could try this mini project for me.

Create a new Unity Project

any way you could like to create this and you will only need one scene to two if you really want to check this works for me. ![|690x394](upload://pDNpAtoLbECuRAjpVZc0CYiqjaE.png)

Adding what we need to test

then we are going to save the scene so we have a level in our assets folder and then add a empty game object. ![|690x370](upload://g7JESOLmgyoa0RnUIPjBcTJawlW.png)

following the steps in the image we can then open the script in your favourite editor.

The Code

the first steps we are going to add a using statement:

using UnityEngine.SceneManagement;

now we need an awake function:

void Awake()
{
SceneManager.sceneLoaded += SceneManager_sceneLoaded;
}

this tells unity what we need to use when the scene is loaded.

this is the new function that replaces OnLevelWasLoaded()

private void SceneManager_sceneLoaded(Scene scene, LoadSceneMode mode)
{
    Debug.Log("LEVEL LOADED");
}

here is how the code looks when complete.

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

public class LevelManager : MonoBehaviour {

    void Awake()
    {
        SceneManager.sceneLoaded += SceneManager_sceneLoaded;
    }

    private void SceneManager_sceneLoaded(Scene scene, LoadSceneMode mode)
    {
        Debug.Log("LEVEL LOADED");
    }
}

and thats it save and run the project in unity once the level loads check the console. you should see the console show the line level loaded.

now if you want to try this in previous personal projects when you are running 5.4 and implement it.

and if you could let me know if this works for you as i would like to know if this is the right way and then if anyone else comes across this it could help them move on quicker as this is a tricky subject as no documentation has been made for this function.

  • Worked
  • Didn’t Work

0 voters

thank you everyone who got to read this and voted please share as well so that we can spread this thread if it works so that other people don’t get stuck.

1 Like

Great job Daniel, thanks for sharing. I hope that another student tries it for you soon. It would be handy to use code formatting where possible above.

Thanks ben I appreciate you looking and commenting. I will try and edit this post with better code brackets when I am back at my computer.

1 Like

Hello restlessdan.

Thx for the info and it worked.
Had this deprecated-message just in GlitchGarden-Project now, and I dont like red-messages or messages with exclamation-marks in console while running a programm.
Yes your code works.
Maybe its important to say, that SceneManager_sceneLoaded() method is called before the Start() method.
Important, because we did some initialisation-stuff in the MusicManager in Glitch Garden.

void Start(){
		audioSource = GetComponent<AudioSource> ();
	}

But this is called after the “Level loaded”.
So the init of the audioSource needs to be put to Awake() or OnEnable().
(Its also possible to put a OnEnable() and a On Disable() method in between, which is automatically called after Awake() and before Scenemanager_sceneLoaded.)

I think the sequence of these new functions is:
Awake()->OnEnable()->SceneLoaded()*->Start()

So this is how I implemented my new MusicManager in GlitchGarden:

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


public class MusicManager : MonoBehaviour {

	public AudioClip[] levelMusicChangeArray;
	private AudioSource audioSource;
	// Use this for initialization
	void Awake () {
		DontDestroyOnLoad (gameObject);
		Debug.Log (name + ":Awake()");

	}

	/*OLD CODE OnLevelWasLoaded deprecated

	void Start(){
		audioSource = GetComponent<AudioSource> ();
	}

	void OnLevelWasLoaded(int level){
		AudioClip actualLevelMusic = levelMusicChangeArray [level];
		Debug.Log (name + ":OnLevelWasLoaded is playing " + actualLevelMusic);
		audioSource.clip = actualLevelMusic;
		audioSource.loop = true;
		audioSource.Play ();
	}
	*/

	void OnEnable(){
		Debug.Log (name + ":OnEnable()");
		audioSource = GetComponent<AudioSource> ();
		SceneManager.sceneLoaded += OnLevelFinishedLoading;
	}


	void OnDisable(){
		SceneManager.sceneLoaded -= OnLevelFinishedLoading;
	}

	void OnLevelFinishedLoading(Scene scene,LoadSceneMode mode){
		int level = scene.buildIndex;
		AudioClip actualLevelMusic = levelMusicChangeArray [level];
		Debug.Log (scene.name + ":OnLevelFinishedLoading is playing " + actualLevelMusic);
		audioSource.clip = actualLevelMusic;
		audioSource.loop = true;
		audioSource.Play ();
	}
}
2 Likes

thanks for sharing and adding.

hopefully the more people who get to this version of unity or start the course with the latest version and then get stuck can get all the help that they need here.

1 Like

Thanks a lot . It worked like a charm.

1 Like

Thanks alot! i’ve stucked on the “AudioClip actualLevelMusic = levelMusicChangeArray [level];” and your solution does work like a charm =))

Thanks for creating this community. Glad to be able to see this gamedev thread from google search engine and saw Phrobion’s code . Life safer. Can sleep…finally…

2 Likes