Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

SceneManager.LoadSceneAsync freezes current level while loading other level

Discussion in 'Scripting' started by Abhinav91, Feb 13, 2016.

  1. Abhinav91

    Abhinav91

    Joined:
    Oct 21, 2013
    Posts:
    67
    Hey everyone, hope ya'll are well.
    So, the documentation for SceneManager.LoadSceneAsync says that it loads the scene asynchronously in the background. I assumed that this meant the current level wouldn't be affected. But for a short period of time (2 or 3 seconds), the current level does freeze up.
    I tried experimenting. I have a simple scene with a Plane, a Slider and a Text object. I have written a script with an IF condition checking if the Space key has been pressed. If it has, another script is enabled. This is the code for the script:

    Code (csharp):
    1.  
    2. public Slider progBar;
    3. public GameObject continueText;
    4. AsyncOperation ao;
    5.  
    6. void OnEnabled()
    7. {
    8. StartCoroutine(LoadTheLevel());
    9. }
    10.  
    11. IEnumerator LoadTheLevel()
    12. {
    13. ao = new AsyncOperation()
    14. ao = SceneManager.LoadSceneAsync(1, LoadSceneMode.Additive); // 1 is the index of the next level.
    15. ao.allowSceneActivation = false;
    16.  
    17. while(!ao.isDone)
    18. {
    19. progBar.value = ao.progress;
    20. Debug.Log(ao.progress);
    21.  
    22. if(ao.progress == 0.9f)
    23. {
    24. continueText.SetActive(true); // It is initially disabled
    25. if(Input.GetKeyDown(KeyCode.F))
    26. {
    27. ao.allowSceneActivation = true;
    28. progBar.gameObject.SetActive(false);
    29. continueText.SetActive(false);
    30. }
    31. }
    32. yield return null;
    33. }
    34. progBar.value = ao.progress;
    35. Debug.Log(ao.progress);
    36. }
    37.  
    So, how it works is, when ao.progress reaches 0.9f, the Text object is set to Active. The text says "Press 'F' To Continue". When the player presses F, ao.allowSceneActivation is set to True, allowing the level to show up on the screen.

    What confuses me is, if LoadSceneAsync loads the level in the background, why does the game freeze for 2 or 3 seconds while the other level is loading?

    I added a Cube to the scene, added a Rigidbody to it, and wrote a small script to move the cube by adding force to it, and also measure it's velocity. Both tasks are being done in the Update method. So when the game is played, the cube starts moving, and the velocity can be seen changing. Then when is hit the Space key, as expected, the next level starts loading. Again, the same freeze of 2 or 3 seconds happens, and the cube freezes too, but once the game gets out of that freeze, the cube starts moving again, only this time, from a much lower velocity.

    If it was just a freeze, how did the velocity go down?
     
  2. Abhinav91

    Abhinav91

    Joined:
    Oct 21, 2013
    Posts:
    67
    Anyone?
     
  3. dhiegolucio

    dhiegolucio

    Joined:
    Mar 2, 2016
    Posts:
    6
    set the yield return null inside the while. It will work o/
     
  4. Abhinav91

    Abhinav91

    Joined:
    Oct 21, 2013
    Posts:
    67
    It is already within the while loop, and it's working perfectly fine. That's not what I was talking about.