OnLevelWasLoaded is deprecated

Hello,

I’m using the latest version of Unity (5 something) and it’s telling me OnLevelWasLoaded is deprecated. But I could not find any documentation on that anywhere… I could however find a lot of unhappy people complaining about the deprecation and speculating on some very complicated ways to get around it!!!

Would anyone know how to sort this out?

Thanks a lot in advance,

Ben

4 Likes

Hi Ben,

I’m guessing you are already using SceneManagement, e.g. using SceneManagement; declared at the top of your class?

Assuming so, with the SceneManager you can provide a method to sceneLoaded which will be called once a scene is loaded (a delegate) e.g.

SceneManager.sceneLoaded += MyMethod;

Once a scene is loaded your MyMethod() method will be called.

private MyMethod(Scene scene, LoadSceneMode mode) {
    // do something awesome
}

A post here:

http://answers.unity3d.com/questions/1160006/how-to-add-a-delegate-to-this-to-get-notifications.html

Documentation:
https://docs.unity3d.com/ScriptReference/SceneManagement.SceneManager-sceneLoaded.html

4 Likes

Hi all,

if you like, you can unblur my complete working class code below.

:slight_smile:

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

public class MusicManager : MonoBehaviour {

    public AudioClip[] levelMusic;
    private AudioSource audioSource;

    void Awake()
    {
        DontDestroyOnLoad(gameObject);
        Debug.Log("Don't destroy " + gameObject.name + " on scene load");
    }


    // Use this for initialization
    void Start () {
        audioSource = gameObject.GetComponent<AudioSource>();
        SceneManager.sceneLoaded += OnLevelLoaded;
    }

    void OnLevelLoaded(Scene scene, LoadSceneMode mode)
    {
        Debug.Log("Playing clip " + levelMusic[scene.buildIndex]);
        AudioClip audioClip = levelMusic[scene.buildIndex];

        if(audioClip)
        {
            audioSource.clip = audioClip;
            audioSource.loop = true;
            audioSource.Play();
        }

    }
}
6 Likes

I’ve implemented this code, and it works. However, I feel like I have no understanding of how the += OnLevelLoaded part works. . How does doing += on a method without specifying inputs result in anything?

Hi,

The += just something plus something else.

What you have here is a delegate, you add the name of the method and it’s effectively subscribed to notificiations. In this case, once the scene has loaded, all methods subscribed to sceneLoaded will be call at once. So here the private void OnLevelLoaded gets called.


See also;

1 Like

Ok I think i understand. SceneManager.sceneLoaded is a delegate that is part of Scene Manager. Doing += OnLevelLoaded adds the method to be called whenever the sceneLoaded delegate is called. The sceneLoaded delegate is called automatically by Unity(not explicitly in the script). I’m assuming the reason I got an error when i tried to do SceneManager.sceneLoaded = OnLevelLoaded is because there are already some invisible methods that are part of the sceneLoaded delegate. Is that correct?

If everything I’m thinking above is correct then isn’t the Unity Scripting API page for sceneLoaded a little misleading? It says “Add a delegate to this to get notifications when a scene has loaded”. Isn’t sceneLoaded really a delegate and you’re adding methods(not delegates)? Am i getting my terminology confused?

Hi,

sceneLoaded is actually an event.


See also;

Delegates are methods they delegate the work to another method. This makes the methods versatile. Delegates can delegate a task to a method then delegate another task to a different method. You need to understand how the event system works. Unity and C# provide default delegates to the event system. It would be very cumbersome to your code if every possible scenario is covered that is why your code needs to wire up this method to the event system. Rob has provided you with a link to events. While reading about events pay special attention to publishers(they send out the notifications) and subscribers(they listen and wait to be notified).
C# EventHandler: C# Events

2 Likes

Thanks Rob and Greg! between the links on Delegates and Events I believe I understand what’s going on with sceneLoaded. I still feel Unity should have more than one line on sceneLoaded explaining it, but maybe that’s just because I’m new to this. I appreciate the responses and links. :slight_smile:

2 Likes

I would agree, that specific item is fairly poorly documented. Only it’s name really give much of an indication.

I’ve used this exact code but the music still restarts whenever a new scene is loaded? Is this the behavior you’re getting?

Ah Nevermind I just realized that every element in the array does not need to have an associated clip.

1 Like

So i m using the code that was posted by Andreas it plays the music fine but if I go to another scene like options it just plays the music from the start again, I’m so confused??

Privacy & Terms