LoadSceneAsync: code after the scene is done loading isn't called

Hello,

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?

    #region Singleton
    static SceneController instance;
    public static SceneController Instance {
        get { return instance; }
    }
    #endregion

void Awake()
    {
        #region Singleton
        if (instance != null && instance != this)
        {
            Destroy(this.gameObject);
        } else {
            instance = this;
            DontDestroyOnLoad(this);
        }
        #endregion
    }

Any help?
Thank you for your time!

Kind regards

I’m not sure about that singleton implementation… Does it still exist in the scene? I mean it’s trivial to look!

Some super-simple Singleton examples to take and modify:

Simple Unity3D Singleton (no predefined data):

https://pastebin.com/SuvBWCpJ

Unity3D Singleton with a Prefab (or a ScriptableObject) used for predefined data:

https://pastebin.com/cv1vtS6G

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.

Thank you for your answer.

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!

Are you sure it’s the same one and not a fresh one?

Before changing the scene, rename the existing one, see if it’s really the same one next scene.