Search Unity

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

Discussion in 'Editor & General Support' started by Jihaysse, May 4, 2021.

  1. Jihaysse

    Jihaysse

    Joined:
    Mar 29, 2020
    Posts:
    53
    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:

    Code (CSharp):
    1.     public IEnumerator SwitchScene(int newSceneIndex)
    2.     {
    3.         previousScene = SceneManager.GetActiveScene();  
    4.         AsyncOperation sceneLoading = SceneManager.LoadSceneAsync(newSceneIndex);
    5.         loadingScreen.gameObject.SetActive(true);
    6.         while (!sceneLoading.isDone)
    7.         {
    8.             loadingScreen.SetLoadingBarValue(Mathf.Clamp01(sceneLoading.progress / .9f));
    9.             yield return null;
    10.         }
    11. // NOTHING OF THIS IS CALLED
    12.         loadingScreen.gameObject.SetActive(false);
    13.         Debug.Log("scene loading done");
    14.         currentScene = SceneManager.GetActiveScene();
    15.     }
    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?

    Code (CSharp):
    1.     #region Singleton
    2.     static SceneController instance;
    3.     public static SceneController Instance {
    4.         get { return instance; }
    5.     }
    6.     #endregion
    7.  
    8. void Awake()
    9.     {
    10.         #region Singleton
    11.         if (instance != null && instance != this)
    12.         {
    13.             Destroy(this.gameObject);
    14.         } else {
    15.             instance = this;
    16.             DontDestroyOnLoad(this);
    17.         }
    18.         #endregion
    19.     }
    Any help?
    Thank you for your time!

    Kind regards
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,744
    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:

    Code (csharp):
    1. public void DestroyThyself()
    2. {
    3.    Destroy(gameObject);
    4.    Instance = null;    // because destroy doesn't happen until end of frame
    5. }
    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.
     
  3. Jihaysse

    Jihaysse

    Joined:
    Mar 29, 2020
    Posts:
    53

    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!
     
    Last edited: May 4, 2021
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,744
    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.