1
\$\begingroup\$

I upgraded my unity project to 2018.3.11f. Since that upgrade, the OnSceneLoaded event is not getting called, even though I am registering the event handler.

Here's the class:

public class SceneSwitcher : MonoBehaviour
{
    void OnSceneLoaded(Scene scene, LoadSceneMode mode)
    {
        print("scene loaded: " + scene.name);
    }

    void Start()
    {
        print("SceneSwitcher.Start() called");
        print("adding event handler to SceneManager");
        SceneManager.sceneLoaded += OnSceneLoaded;
    }
}

here's the console output:

SceneSwitcher.Start() called
UnityEngine.MonoBehaviour:print(Object)
SceneSwitcher:Start() (at Assets/Scripts/SceneSwitcher.cs:45)
adding event handler to SceneManager
UnityEngine.MonoBehaviour:print(Object)
SceneSwitcher:Start() (at Assets/Scripts/SceneSwitcher.cs:46)

scene loaded: 3 is never in the console output.

I see others have had this problem, with no answers.

This code worked before the upgrade. Now I am stuck. Not sure what I can do. Any help is appreciated.

Thnx

\$\endgroup\$
  • \$\begingroup\$ perhaps because in the snippet you show, there is no changing of scene request \$\endgroup\$ – BugFinder Apr 5 '19 at 9:55
  • \$\begingroup\$ I moved the code from Start() to Awake(). Now it works. It seems like order of events behind the scenes might have changed or something. I will post this as the answer. If someone can help me understand why that changed worked, that would be great. (to verify my change worked, I moved it back to Start() and the sceneloaded event was not called). Awake() is the correct place for this now. \$\endgroup\$ – tatmanblue Apr 5 '19 at 14:02
  • \$\begingroup\$ @BugFinder fair point. I am switching scenes (and I believe before the upgrade the event was called even during my first scene). I do not know how to "prove" that concisely enough. If you have a suggestion what else to include, I am happy to add it. \$\endgroup\$ – tatmanblue Apr 5 '19 at 14:23
  • \$\begingroup\$ All i can say is code order is a thing. \$\endgroup\$ – BugFinder Apr 6 '19 at 1:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.