I’m using LoadSceneAsync to create a loading screen and I’m wondering why the code snippet after the while loop isn’t called:
public IEnumerator SwitchScene(int newSceneIndex)
{
previousScene = SceneManager.GetActiveScene();
AsyncOperation sceneLoading = SceneManager.LoadSceneAsync(newSceneIndex);
loadingScreen.gameObject.SetActive(true);
while (!sceneLoading.isDone)
{
loadingScreen.SetLoadingBarValue(Mathf.Clamp01(sceneLoading.progress / .9f));
yield return null;
}
// NOTHING OF THIS IS CALLED
loadingScreen.gameObject.SetActive(false);
Debug.Log("scene loading done");
currentScene = SceneManager.GetActiveScene();
}
I’ve first thought that my SceneController script wasn’t persisted between scenes so the coroutine was stopped, but I’m using a Singleton with a DontDestroyOnLoad, so it should still exists right?
These are pure-code solutions, do not put anything into any scene, just access it via .Instance!
If it is a GameManager, when the game is over, make a function in that singleton that Destroys itself so the next time you access it you get a fresh one, something like:
public void DestroyThyself()
{
Destroy(gameObject);
Instance = null; // because destroy doesn't happen until end of frame
}
Either way, put in lots of Debug.Log() statements as well as some in OnDisable() in your singleton above to make sure it isn’t going away.
The SceneController (this object) is indeed still existing in my newly loaded scene.
I tried using a Debug.Log in OnDisable(), but it isn’t called (except when I stop the game of course).
EDIT: I found the issue, I was actually starting the Coroutine SwitchScene from an object which wasn’t persisted through scene changes.
Thanks for the help!