Capture reference to loaded scene

Hey folks,

I was wondering if there was anyway to capture a reference to a recently loaded Scene?

I’m using:

SceneManager.LoadSceneAsync("RockPrototype", LoadSceneMode.Additive)

To load up a new scene, now because I’ve got several instances of the “RockPrototype” scene, I can’t simple find it by name and even it’s BuildIndex wouldn’t be much use to me. I have considered using the hashcode for each scene as a unique identifier. However if I was going to use this I would need to capture that hashcode at the point I’m loading the scene.

Any thoughts?

Try:

void Awake()
{
    SceneManager.sceneLoaded += this.OnSceneLoaded;
}

void OnSceneLoaded(Scene scene, LoadSceneMode sceneMode)
{
   Debug.Log("New scene loaded: " + scene);
}

Thanks for the reply!

So this doesn’t quite solve my problem, there will be let’s say 20 scenes being loaded up, of varying types. What I want to be able to do is in pseudo:

  • Load Rock scene = newRockScene (this loads the scene and stores a reference to the scene)
  • newRockScene.Rocks = something (now I can do stuff with that scene)

If I was to use your example, I would have several scenes firing the same code all at once.

You can do it the other way around; every gameobject has a reference to its scene.

You can check which scene fired the callback, for example:

switch (scene.name)
{
   case "RockPrototype": DoRockPrototypeStuff(); break;
   case "WoodPrototype": DoWoodPrototypeStuff(); break;
}

(or if you have many scenes, can use a dictionnary to avoid the huge switch block etc)