Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

How come SceneManager.activeSceneChanged have two parameters?

Discussion in 'Scripting' started by thirdcoke, Sep 7, 2016.

  1. thirdcoke


    Nov 2, 2015
    My unity version is 5.4.0f3. I want to implement a scene manager , which need to subscribe a event that will be raised when active scene has been changed. So I found SceneManager.activeSceneChanged in API reference. But there is no example and nothing about the parameters. I did some tests which is showing that the first parameter is always "null"(not null in C# I mean but refers to no scene, property buildIndex is always -1). I'm not sure if I did wrong way. Can anyone tell me the meaning of the first parameter? I'm appreciated.

    The signature of the event is:
    public static event UnityAction<Scene, Scene> activeSceneChanged;

    And here is my testing code:
    Code (CSharp):
    1. public class SceneTest : MonoBehaviour
    2. {
    3.     void Start()
    4.     {
    5.         DontDestroyOnLoad(this);
    7.         SceneManager.activeSceneChanged += SceneManager_activeSceneChanged;
    8.     }
    10.     private void SceneManager_activeSceneChanged(Scene arg0, Scene arg1)
    11.     {
    12.         Debug.LogFormat("[Arg1]{0} [Arg2]{1}", arg0.buildIndex, arg1.buildIndex);
    13.     }
    15.     void Update()
    16.     {
    17.         if (Input.GetKeyDown(KeyCode.Alpha1))
    18.         {
    19.             Debug.Log("Load Scene 1");
    20.             SceneManager.LoadScene(0, LoadSceneMode.Single);
    21.         }
    22.         if (Input.GetKeyDown(KeyCode.Alpha2))
    23.         {
    24.             Debug.Log("Load Scene 2");
    25.             SceneManager.LoadScene(1, LoadSceneMode.Single);
    26.         }
    27.     }
    28. }
  2. takatok


    Aug 18, 2016
    Did some testing and:
    Arg1 = Previous Active Scene
    Arg2 = Current Active Scene

    When you use a standard LoadScene the old Scene is destroyed, so i think internally rather than giving you a null, it sets = "", buildindex = -1 and so on.

    If you use
    Code (CSharp):
    1. SceneManager.LoadScene("New Scene",LoadSceneMode.Additive);
    No call will be made at all. Because Additive loading just adds the NewScene , but keeps the Original Scene as the Active Scene.
    However if you then do this:
    Code (CSharp):
    1. Scene scene = SceneManager.GetSceneByName("New Scene");
    2.         SceneManager.SetActiveScene(scene);
    You changed scenes so activeChangedScene callback will get called:

    Arg1 = buildIndex = 0 , name = Original Scene <-- Old Active Scene
    Arg2 = buildIndex = 1, name = New Scene <--- Current Active Scene
    MGGDev, Anisoropos and Zullar like this.
  3. thirdcoke


    Nov 2, 2015
    Yeah I think you're right. I noticed that the documentation is saying "Run-time data structure for *.unity file.".
    So the "reference"(Actually it's a struct, or let's say "copy") to a specific scene you kept in your script will become "null"(yeah looks weird, maybe Unity replace your copy with a null struct) once Unity unload the scene.

    This make sense at least, thanks!

  4. Baste


    Jan 24, 2013
    You should complain about this kind of stuff in the documentation section - they're pretty good at getting it changed.

    I did it for you this time.
  5. thirdcoke


    Nov 2, 2015
    Yeah I see it, thank you!