- Home /
Get a reference to the newly loaded scene after a LoadSceneAsync()
How can I get a reference to the new Scene
object loaded after the AsyncOperation
returned by SceneManager.LoadSceneAsync()
completes?
Answer by brain56 · Mar 16, 2016 at 07:24 PM
You can use this code snippet:
Scene reference;
bool isLoading;
IEnumerator LoadScene(string sceneName)
{
this.isLoading = false;
AsyncOperation op = SceneManager.LoadSceneAsync(sceneName, LoadSceneMode.Additive);
while (this.isLoading)
{
if (op.isDone)
{
this.isLoading = false;
this.reference = SceneManager.GetSceneByName(sceneName);
}
yield return 0;
}
}
Or you can go with the built-in delegate
sceneWasLoaded found in the SceneManager.
This will only work if there is only one instance of a scene with 'sceneName'. So for multiple scenes with the same name this won't work.
For this to work I think isLoading should be initialised as True.
You can simply use operation.Completed callback
op.Completed += handle =>
{
Scene yourScene = handle.Result.Scene;
}
Answer by Aedit · Feb 17, 2020 at 01:05 AM
The easiest way to get a reference to the last scene you have loaded is to keep track of the total scene count. You can use this to call SceneManager.GetSceneAt(index). So in short:
int Index = SceneManager.sceneCount;
var operation = SceneManager.LoadSceneAsync(spawnedSceneName, LoadSceneMode.Additive);
operation.completed += (s) =>
{
Scene myScene = SceneManager.GetSceneAt(Index);
};
Your answer
![](https://koobas.hobune.stream/wayback/20220612072107im_/https://answers.unity.com/themes/thub/images/avi.jpg)