Another way to fix this Health bug

when print healthPoints at Start(), RestoreState(), the logger will show healthPoints at Start() twice times, and at RestoreState() one time. But, it shows first time at Start(), then another time at RestoreState(), finally another time at Start().
So I change code of SavingSystem.cs to make RestoreState() will be called at the end after the scene is actually loaded


Reference: Unity - Scripting API: SceneManagement.SceneManager.LoadSceneAsync
in Portal.cs:

yield return SceneManager.LoadSceneAsync(SceneToLoad);

=>

AsyncOperation asyncLoad = SceneManager.LoadSceneAsync(SceneToLoad);

        while (!asyncLoad.isDone)

        {

            yield return null;

        }
1 Like

Again in Portal.cs:

yield return SceneManager.LoadSceneAsync(SceneToLoad);

=>

AsyncOperation asyncLoad = SceneManager.LoadSceneAsync(SceneToLoad);

        while (!asyncLoad.isDone)

        {

            yield return null;

        }

It was my understanding that public static AsyncOperation LoadSceneAsync(int sceneBuildIndex, SceneManagement.LoadSceneMode mode = LoadSceneMode.Single); waited until the scene was loaded since it returns the Async Operation

you can yield until asynchronous operation continues, or manually check whether it’s done (isDone) or progress (progress).

The reason to cache the Async operation is if you want to do other things while the scene is loading, i.e. update a progress bar of the percentage of the scene being loaded, update text on the screen like tips or lore about the game while the game is loading. Am i wrong in this conclusion?

Yeah, to me those do the same thing. I’ve only ever used the AsyncOperation to

  1. Update a progress bar, and
  2. Preload a scene

That’s all I’ve used it for… loading progress bars. Truth be told, though, if you stick with small scenes, a loading progress bar won’t display very long.

My conclusion that yield return SceneManager.LoadSceneAsync(SceneToLoad); is the same as

while (!asyncLoad.isDone)
{
           yield return null;
}

The only difference is we can do extra stuff in the while loop. But they both wait until the scene is loaded before continuing.

1 Like

Privacy & Terms