How come SceneManager.activeSceneChanged have two parameters?

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:

public class SceneTest : MonoBehaviour
{
    void Start()
    {
        DontDestroyOnLoad(this);

        SceneManager.activeSceneChanged += SceneManager_activeSceneChanged;
    }

    private void SceneManager_activeSceneChanged(Scene arg0, Scene arg1)
    {
        Debug.LogFormat("[Arg1]{0} [Arg2]{1}", arg0.buildIndex, arg1.buildIndex);
    }

    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Alpha1))
        {
            Debug.Log("Load Scene 1");
            SceneManager.LoadScene(0, LoadSceneMode.Single);
        }
        if (Input.GetKeyDown(KeyCode.Alpha2))
        {
            Debug.Log("Load Scene 2");
            SceneManager.LoadScene(1, LoadSceneMode.Single);
        }
    }
}

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 Arg1.name = “”, buildindex = -1 and so on.

If you use

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:

Scene scene = SceneManager.GetSceneByName("New Scene");
        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

3 Likes

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!

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.

Yeah I see it, thank you!