How to use SceneManager.onSceneLoaded ?

I’ve upgraded to Unity 5.4b15 and now i have a lot of warning saying that OnLevelWasLoaded is deprecated.
I’ve tried to find SceneManager.onSceneLoaded on the 5.4 doc but it’s not complete and there is no script exemple.
Anyone know how SceneManager.onSceneLoaded works ?

2 Likes

It’s an event. Append a delegate to it to receive callbacks.

Knowing Unity, it’s probably of type System.Action (you can righclick it in Visual Studio or MonoDevelop and click goto definition to see). So it’d be something along the lines of:

void Start()
{
    SceneManager.sceneLoaded += OnSceneLoaded;
}

void OnSceneLoaded()
{
    //do stuff
}
3 Likes

Actually you have to pass in parameters into the delegate. I believe it has a Scene and a SceneMode

2 Likes

this worked for me to do something every time a new scene loaded

void Update () {
SceneManager.sceneLoaded += OnSceneLoaded;
}

void OnSceneLoaded (Scene scene, LoadSceneMode mode) {
//do stuff
}

4 Likes

This doesn’t work for me because the timing of the event that gets fired for SceneManager.sceneLoaded is different to that of OnLevelWasLoaded. In my case the scene I just loaded has definitely not finished loading when this new call back is called and I get null reference exceptions when I try to access object that should be in the scene but aren’t (yet). We have a discussion thread on this here …

Unity are looking at it but we need others to add their voices to the discussion to get it noticed and fixed.

2 Likes

I used the advice above (thank you everyone!) but still had to play around with the SceneManagement to get exactly what I wanted. Below I have posted how I changed my OnLevelWasLoaded() to OnSceneLoaded(). Maybe it will help someone else out!

This is what I had:

And this is what I changed it to:

1 Like

Thanks everyone who helped on this thread! I’ve been trying to find this solution for hours now!

Just an advice. Don’t add the delegate in Update like that, or it will try to add it multiple times, however, adding just once is more than enough.

4 Likes

I ran into the same problem. I got a null reference exception because public text fields defined in the editor were not yet available when OnSceneLoaded() was calling a method in the script.

I, instead, had the Start method from the script with the text fields test if the original object was in the scene, and then run what I was trying to run in OnSceneLoaded from the Start method. e.g.:

public class Messenger : MonoBehaviour {

    public int selectedID;

    public void Start(){
        DontDestroyOnLoad(this);
    }

    public void DoSomething(){
            LoadedScript.ShowID(selectedID);
            DestroyObject(gameObject);
    }
}

And on the newly loaded scene there’s an object with the script:

public class LoadedScript : MonoBehaviour {

private Text[] idText;

    void Start () {
        if (GameObject.Find("Messenger")){
            Messenger messenger = GameObject.FindObjectOfType<Messenger>();
            messenger.DoSomething();
        }
}

    ShowID(int selectedID){
        idText.text = selectedID;
    }

}