I created a separate scene for UI and managers, and individual scenes for game levels. At runtime, I load them together additively.
When the player dies, I want to reload the level scene.
Currently, my code looks like this:
public UnityEvent<string> OnReload;
private void Reset()
{
OnReload?.Invoke(""); // triggers the loading animation
Invoke(nameof(UnloadAndReload), 1.35f);
}
private void UnloadAndReload()
{
SceneManager.UnloadSceneAsync(CurrentLevel.SceneReference.BuildIndex).completed += operation =>
{
SceneManager.LoadScene(CurrentLevel.SceneReference.BuildIndex, LoadSceneMode.Additive);
};
}
The UnityEvent triggers a loading animation, which I use to hide the screen blink during scene reloading.
But I’m not sure if this is the best practice.
My questions are:
- Is it necessary to call
SceneManager.LoadSceneafterUnloadSceneAsync.completed?
Or is it fine to just write it like this?
private void UnloadAndReload()
{
SceneManager.UnloadSceneAsync(CurrentLevel.SceneReference.BuildIndex);
SceneManager.LoadScene(CurrentLevel.SceneReference.BuildIndex, LoadSceneMode.Additive);
}
- What exactly happens when I call
UnloadSceneAsync?
- Are all GameObject references in that scene set to
nullimmediately? - Could it happen that the same scene exists twice in memory at the same time? (I’m worried because I still use
Find()to get references, and I don’t want duplicate objects to cause issues or crashes.) - What parts of the scene are actually unloaded asynchronously in backend?