Search Unity

SceneManager.activeSceneChanged never called

Discussion in 'Scripting' started by sergiusz308, Aug 23, 2016.

  1. sergiusz308

    sergiusz308

    Joined:
    Aug 23, 2016
    Posts:
    235
    Hi people, I have following setup:

    MainScene: GO with script that initiates loading other scene with the code:

    Code (CSharp):
    1. void Start ()
    2.     {
    3.         SceneManager.sceneLoaded += SceneManager_sceneLoaded;
    4.         SceneManager.activeSceneChanged += SceneManager_activeSceneChanged;
    5.         SceneManager.LoadScene(2, LoadSceneMode.Additive);
    6.     }
    SceneManager_sceneLoaded is called and then I do:

    Code (CSharp):
    1. void SceneManager_sceneLoaded(Scene arg0, LoadSceneMode arg1)
    2.     {
    3.         SceneManager.SetActiveScene(arg0);
    4.     }
    SetActiveScene returns "false" and activeSceneChanged is never called.

    How should I chain these events to make it work: I'd like to load scene, and once it's loaded to activate it and then do things to loaded scene objects?

    Edit: I'm working with latest Unity version 5.4.0f3
     
  2. ev3d

    ev3d

    Joined:
    Apr 19, 2013
    Posts:
    327
    How did you fix this?
     
  3. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,338
    SceneManager.activeSceneChanged is called when you change which scene is active when several scenes are loaded at once (through additive loading)... I believe.

    Honestly, the documentation on the SceneManager stuff is very sparse. It mentions the active scene a bunch, without explaining what that means.
     
  4. takatok

    takatok

    Joined:
    Aug 18, 2016
    Posts:
    1,496
    Ok I created 2 simple Scenes and added a GameObject to Scene one and attached this script:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.Events;
    4. using UnityEngine.EventSystems;
    5. using UnityEngine.UI;
    6. using UnityEngine.SceneManagement;
    7.  
    8. public class ActionTest : MonoBehaviour
    9. {
    10.    void Start()
    11.     {
    12.         SceneManager.sceneLoaded += SceneManager_sceneLoaded;
    13.         SceneManager.activeSceneChanged += SceneManager_activeSceneChanged;
    14.         SceneManager.LoadScene("Scene2", LoadSceneMode.Additive);
    15.     }
    16.  
    17.     void SceneManager_sceneLoaded(Scene arg0, LoadSceneMode arg1)
    18.     {
    19.         if (SceneManager.SetActiveScene(arg0))
    20.             Debug.Log(arg0.name + " set to Active:  Success");
    21.         else
    22.             Debug.Log(arg0.name + " set to Active: Failed");
    23.     }
    24.  
    25.     void SceneManager_activeSceneChanged(Scene arg0, Scene arg1)
    26.     {
    27.         Debug.Log("Active Scene Changed");
    28.     }
    29. }
    I get a failed debug message. So like many things with Unity when there is a lot of stuff going on in the background I decided to wait 1 Frame then try with this code:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.Events;
    4. using UnityEngine.EventSystems;
    5. using UnityEngine.UI;
    6. using UnityEngine.SceneManagement;
    7.  
    8. public class ActionTest : MonoBehaviour
    9. {
    10.    void Start()
    11.     {
    12.         SceneManager.sceneLoaded += SceneManager_sceneLoaded;
    13.         SceneManager.activeSceneChanged += SceneManager_activeSceneChanged;
    14.         SceneManager.LoadScene("Scene2", LoadSceneMode.Additive);
    15.     }
    16.  
    17.     void SceneManager_sceneLoaded(Scene arg0, LoadSceneMode arg1)
    18.     {
    19.         StartCoroutine(LoadScene(arg0));
    20.     }
    21.  
    22.     void SceneManager_activeSceneChanged(Scene arg0, Scene arg1)
    23.     {
    24.         Debug.Log("Active Scene Changed");
    25.     }
    26.  
    27.     IEnumerator LoadScene(Scene scene)
    28.     {
    29.         yield return null;
    30.         if (SceneManager.SetActiveScene(scene))
    31.             Debug.Log("It worked");
    32.         else
    33.             Debug.Log("It didn't");
    34.     }
    35. }
    This Gave me the Debug Message: "It Worked" as well as "Active SCene Changed".
    So the trick is just stall 1 frame in a coroutine then change your active scene. Apparently Unity is sending the SceneLoaded Event a little to soon. Something isn't totally setup yet, and SceneManager.SetActive() is failing
     
  5. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,338
    That you can't react to an additive scene load by setting the scene active is either a bug, or horrendous design. I recommend that you send a bug report.