Suggestion for async scene loading problem

Hi guys,

I have experienced some issues with the transform.position of the player to the spawn point and figured out that this did simply not work because the scene was not loaded entirely when the transform.position assignment happens. So in order to prevent that, you might want to change the code to something like this:

  AsyncOperation loadingScene = SceneManager.LoadSceneAsync(sceneToLoad);
            while (!loadingScene.isDone)
            {
                yield return null;
            }
            
            Portal otherPortal = GetOtherPortal();
            UpdatePlayer(otherPortal);

Here is a link to the docs:

While this will definitely work, I’m surprised that

yield return SceneManager.LoadSceneAsync(sceneToLoad);

was not working.

What sort of behavior were you experiencing? Were there any error messages?

Hi Brian, thanks for the reply. So basically I had the issue that the part where we set the players transform position to the spawn point position just didn’t work in combination with the yield return SceneManager.LoadSceneAsync(sceneToLoad).

So this method here had no influence at all on the players position:

 private void UpdatePlayer(Portal otherPortal)
        {
            var player = GameObject.FindWithTag("Player");
            player.transform.position = otherPortal.spawnPoint.position;
            player.transform.rotation = otherPortal.spawnPoint.rotation;
        }

The player was always spawned where the gameobject is initially placed in the scene. As soon as I changed the code to the approach with the while loop from the unity docs, the player was positioned correctly to the according spawn point of the portal. So I’m not quite sure why it didn’t do the job with yield return SceneManager.LoadSceneAsync but it appeared to me that it just didn’t wait until the scene was completely loaded. I have to say that my scene is way more loaded with meshes / objects, so maybe this will just come up as an issue if your scene has heavier loading times. Definitely something that would require more testing with different scenes and loading times, in order to identify the problem. From my understanding yield return SceneManager.LoadSceneAsync will return the AsyncOperation to the Coroutine, I’m not sure if it will actually wait until that AsyncOperation is done or what the implemented behavior of unity is on that side. I couldn’t find anything in the docs either.

EDIT:
Also there was no error message, so it just didn’t change the position of the player. I haven’t change anything in the UpdatePlayer method, just the part with the yield return LoadSceneAsync

yield return SceneManager.LoadSceneAsync(scene);

Should block the coroutine until the scene load is finished.

I just ran a few tests with debugs to confirm it is working properly in the course project.

Regardless, the method you’ve outlined above (from the documentation sample) will always wait until the scene is loaded, so as long as that’s working, I’d say stick with it. (If it ain’t broke).

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms