Search Unity

  1. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice
  2. Ever participated in one our Game Jams? Want pointers on your project? Our Evangelists will be available on Friday to give feedback. Come share your games with us!
    Dismiss Notice

LoadSceneAsync problem

Discussion in 'Scripting' started by BlueReptile, May 30, 2020.

  1. BlueReptile

    BlueReptile

    Joined:
    May 30, 2020
    Posts:
    3
    I am trying to solve this for over 1 month and still not getting it done. I've been trying a lot of different methods and the result is still the same.


    Code (CSharp):
    1.     public void OnTriggerEnter()
    2.     {
    3.         LoadingObject.active = true;
    4.         StartCoroutine(LoadAsyncOperation(SceneManager.GetActiveScene().buildIndex + 1));
    5.     }
    6.  
    7.     IEnumerator LoadAsyncOperation(int sceneindex)
    8.     {
    9.         AsyncOperation gameLevel = SceneManager.LoadSceneAsync(sceneindex);
    10.  
    11.         while(gameLevel.isDone != true)
    12.         {
    13.             yield return null;
    14.         }
    15.     }
    This is just one method. Anyway... The problem is that when I finish the level my loading animation doesn't show and instead it moves to the next level right away. And that's not all. I get 6-8 scenes with (is loading) and if I try to play I can't until all the scenes load.

    If anyone can tell me what to do and help me that would be really great. As i said, This problem pissed me off for over 1 month.
     
  2. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,023
    That's really very unclear. How do you know how many scenes are loading? What does it mean to "try to play" before they load? Where is this animation that you say isn't playing? What is "LoadingObject"? Why is your coroutine waiting in a loop but then not doing anything at the end of the wait?

    I don't see anything in there that would load more than one scene, so if you're sure about that part, then I can only guess that this is running multiple times (maybe several objects enter the trigger, or one object enters and leaves and re-enters) or you have additional scripts that are also trying to load scenes.
     
    BlueReptile likes this.
  3. WarmedxMints

    WarmedxMints

    Joined:
    Feb 6, 2017
    Posts:
    921
    What I would do is set a bool just in case your method is being called more than once. You can also prevent the scene from activating until you want it too.

    Code (CSharp):
    1.     private bool _loading = false;
    2.  
    3.     public void OnTriggerEnter(Collider other)
    4.     {
    5.         //Check if we are loading
    6.         if (_loading)
    7.             return;
    8.  
    9.         _loading = true;
    10.  
    11.         LoadingObject.active = true;
    12.         StartCoroutine(LoadAsyncOperation(SceneManager.GetActiveScene().buildIndex + 1));
    13.     }
    14.  
    15.     IEnumerator LoadAsyncOperation(int sceneindex)
    16.     {
    17.         AsyncOperation gameLevel = SceneManager.LoadSceneAsync(sceneindex);
    18.  
    19.         gameLevel.allowSceneActivation = false;
    20.  
    21.         while (!gameLevel.isDone)
    22.         {
    23.             if (gameLevel.progress >= 0.9f)
    24.             {
    25.                 //You wait here until you animation is finished before loading the level
    26.                 //You can check if it is finished or simply wait for x time like so
    27.                 yield return new WaitForSeconds(5f);
    28.                 //Now we allow the scene to load
    29.                 sceneAO.allowSceneActivation = true;
    30.             }
    31.             yield return null;
    32.         }
    33.     }
     
    BlueReptile likes this.
  4. BlueReptile

    BlueReptile

    Joined:
    May 30, 2020
    Posts:
    3
    There are not multiple different scenes.


    "LoadingObject" is an empty game object where i've put my loading assets, animation and canvas.
    By "Try to play" I mean that when i start to play the level won't let me do it because the next "Lvl3" loads and the lvl starts over then.
     

    Attached Files:

  5. BlueReptile

    BlueReptile

    Joined:
    May 30, 2020
    Posts:
    3
    Thank you a lot! I've been struggling with it for so long.
     
unityunity