Search Unity

Application.LoadLevelAsync Will only work once per game

Discussion in 'Scripting' started by AzraelKans, Sep 6, 2011.

  1. AzraelKans

    AzraelKans

    Joined:
    Oct 29, 2009
    Posts:
    135
    Im using Application.LoadLevelAsync to show a loading scene while a large scene is loaded, the problem is that it only works once, the second time I try to load another level from the main menu it stays in the loading scene and never loads.

    I have also tried it without the loading scene and I get the same result. What can I do?

    This is my entire coroutine
    Code (csharp):
    1.     public IEnumerator LoadLevel(string level, int checkpoint){
    2.         //currentLevel=level;
    3.         if (isLoading) return;
    4.         Application.LoadLevel("loading");
    5.         yield return new WaitForSeconds(1.5f);
    6.         currentCheckpoint=checkpoint;
    7.         AsyncOperation ao=Application.LoadLevelAsync(level);
    8.         isLoading=true;
    9.         while(!ao.isDone)
    10.         {
    11.             loadProgress = ao.progress;
    12.             Debug.Log("Load progress "+  loadProgress);
    13.             yield return 0;
    14.         }      
    15.         isLoading=false;
    16.         AfterLevelLoaded();
    17.     }
    18.  
     
  2. bdev

    bdev

    Joined:
    Jan 4, 2011
    Posts:
    656
    either try just doing

    yield return ao; (will wait for the level to load)

    or not yield return 0 but instead

    yield return null; (will wait for next frame)
     
  3. bdev

    bdev

    Joined:
    Jan 4, 2011
    Posts:
    656
    Also make sure you've got this script using DontDestroyOnLoad