3

I'm having trouble understanding why SceneManager.sceneLoaded += OnSceneLoaded; isn't getting called while OnEnable is getting called. Am I doing somthing incorrectly? I'm testing it by looking for a return coming from Debug.Log("Current scene index is: " + scene.buildIndex.ToString()); in the console, but it's not. Any help would be appreciated! Thank you.

// called first
    void OnEnable()
    {
        Debug.Log("OnEnable called");
        SceneManager.sceneLoaded += OnSceneLoaded;
    }

    // called second
    void OnSceneLoaded(Scene scene, LoadSceneMode mode)
    {
        Debug.Log("Current scene index is: " + scene.buildIndex.ToString());

        switch (scene.buildIndex)
        {
            case 1: // Starting Scene
                StartingSceneIn();
                break;

            case 2: // Selection Scene
                SelectionSceneIn();
                break;

            case 3: // Coloring Scene
                ColoringSceneIn();
                break;

            case 4: // Preview Scene
                StartCoroutine(PreviewSceneIn());
                break;

            default:
                break;
        }
    }

    // called when the game is terminated
    void OnDisable()
    {
        Debug.Log("OnDisable");
        SceneManager.sceneLoaded -= OnSceneLoaded;
    }
greyBow
  • 1,298
  • 3
  • 28
  • 62
  • 1
    Is the script enabled? Also, is the GameObject this script is attached to active? – Programmer Apr 11 '18 at 18:09
  • 1
    @Programmer, I’m getting “OnEnabled Called.” In my console when I run it. – greyBow Apr 11 '18 at 18:11
  • 1
    What's your Unity's version? I tested this and it worked fine. Can you put `SceneManagement.SceneManager.sceneLoaded += OnSceneLoaded;` in the `Awake` function and see if it works or not. – Programmer Apr 11 '18 at 18:14
  • I’m on Unity 2017.3.1, I tried it in the Awake function and it still doesn’t work. – greyBow Apr 11 '18 at 18:35
  • Assuming you are right about this, I suspect bug. File for a bug report. Before doing so, I suggest you restart both Unity and Visual Studio then see if that fixes this issue. – Programmer Apr 11 '18 at 19:14

4 Answers4

3

Please check your Project Settings (Edit/Project Settings) - under the Editor tab you will find the Enter Play Mode Settings option.

enter image description here

As long as this option is disabled, or both this and the Reload Scene option are enabled, the sceneLoaded event should be triggered as expected. If you enable Enter Play Mode Options but disable Reload Scene, the editor apparently does not fully "load" the scene which in turn prevents the sceneLoaded event from triggering.

(IIRC disabling this option is one of the recommended settings to speed up switching from editor to play mode but it seems to have unwanted side effects)

1

I had the same problem. Fixed it by not using the Awake function in my GameManager. Not sure why this worked.

stickske
  • 21
  • 4
  • It can't be an answer without explanation and if you are not sure. Please edit it, or mode to comments – Anna Oct 27 '19 at 23:18
0

I got it working by using the following;

UnityEngine.SceneManagement.SceneManager.sceneLoaded += OnSceneLoaded;

With or without awake method.. it looks like we have to add UnityEngine.SceneManagement again.

0

I just found the solution -_- just don't unsubscribe OnSceneLoaded.

remove SceneManager.sceneLoaded -= OnSceneLoaded it will now work