Search Unity

  1. If you have experience with import & exporting custom (.unitypackage) packages, please help complete a survey (open until May 15, 2024).
    Dismiss Notice
  2. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice

Error in build at AsyncOperation.progress but not in builder

Discussion in 'Scripting' started by Vedrit, Feb 11, 2018.

  1. Vedrit

    Vedrit

    Joined:
    Feb 8, 2013
    Posts:
    514
    I've come across some odd behaviour. I have an operation where I'm async additive loading a scene, and then later removing the previous scene. Everything runs flawlessly in the editor, but when I build a client I get NullReferenceExceptions, but only at some scenes.
    Code (csharp):
    1.  
    2. IEnumerator unloadLoad()
    3.     {
    4.         AsyncOperation unload = SceneManager.UnloadSceneAsync("load");
    5.         while (true)
    6.         {
    7.             if(unload == null)
    8.             {
    9.                 Debug.LogError("Unload was null when attempting to unload 'load' from scene '" + nextScene + "'");
    10.                 //Sometimes this will trigger, sometimes not, even if the NullRef below does get thrown. Never thrown when run in editor.
    11.             }
    12.             if(unload.progress >= 0.9f) //NullRef here. Never thrown when run in editor.
    13.             {
    14.                 break;
    15.             }
    16.             yield return null;
    17.         }
    18.     }
    19.  
    I'm using Unity 2017.1.1f1 and I've tried a few variations of the script above.
    Additionally, the scene does actually get unloaded so nothing is -actually- broken. Just, errors are being thrown.