Search Unity

NullReferenceException when using Addressables.DownloadDependenciesAsync(label)

Discussion in 'Addressables' started by MaskedMouse, Jul 2, 2019.

  1. MaskedMouse

    MaskedMouse

    Joined:
    Jul 8, 2014
    Posts:
    389
    When preloading assets through a label using
    Addressables.DownloadDependenciesAsync
    it returns a null task.
    When using
    LoadAssetsAsync<GameObject>
    with the same key(label), it goes just fine and it returns the gameobject asset.

    The GetDownloadSizeAsync won't work either with the label. It always returns 0, probably because it is null as well.


    Minimal reproduction project.
    I've got it set to Packed mode in the editor so it has to "download" the bundles and cache them.
    https://maskedmous.stackstorage.com/s/Q4h6dGA2V2bueUq

    Build Bundles
    Packed mode
    Go into play mode and click the download button. -> receives 0 size
    then after clicking the Yes button it will try to download the dependencies but it returns a null task.

    Code (CSharp):
    1.     public AssetReferenceGameObject PrefabRef;
    2.     [ContextMenu("Delete Cache")]
    3.     public void DeleteCache()
    4.     {
    5.         Caching.ClearCache();
    6.     }
    7.  
    8.     public string Dependency = "Preload";
    9.     public TextMeshProUGUI DownloadText;
    10.  
    11.     public async void GetDownloadText()
    12.     {
    13.         DownloadText.text = $"Are you sure you want to download {((await Addressables.GetDownloadSizeAsync(Dependency).Task) / 1024f / 1024f)} mb of assets?";
    14.     }
    15.  
    16.     public async void OnDownloadButtonClick()
    17.     {
    18.         // Null Reference Exception
    19.         await Addressables.DownloadDependenciesAsync(Dependency).Task;
    20.     }
    21.  
    22.     public async void GetMeForced()
    23.     {
    24.         var asset = await Addressables.LoadAssetAsync<GameObject>(Dependency).Task;
    25.         Instantiate(asset);
    26.     }
     
    Last edited: Jul 2, 2019
    faolad likes this.
  2. faolad

    faolad

    Joined:
    Jan 27, 2013
    Posts:
    60
    Same here, can't figure out how to make it work. Always 0.
     
  3. CristianGarciaJ

    CristianGarciaJ

    Joined:
    Apr 13, 2018
    Posts:
    8
    Hi everybody,

    It's also happening in my end. I always getting a null Task when I try to return it for an asset that doesn't have dependencies (I know that because trying to do GetDependencies() always returns a null exception with a message of no deps). The stacktrace is the following:

    And the strange piece of code for the Tasks that doesn't have an specific result (Task<object>) is the following:

    Code (CSharp):
    1.  
    2. System.Threading.Tasks.Task<object> IAsyncOperation.Task
    3.         {
    4.             get
    5.             {
    6.                 return Task as System.Threading.Tasks.Task<object>;
    7.             }
    8.         }
    9.  
    Is trying to cast a Task but debugging this part I saw that is not initialized yet.

    A workaround seems to be use a Coroutine to await the AsyncOperationHandle but with that you lose the advantages of Tasks.
     
    Last edited: Jul 11, 2019
  4. CristianGarciaJ

    CristianGarciaJ

    Joined:
    Apr 13, 2018
    Posts:
    8
    Hi again,

    Testing a scene (which is supposed to have dependencies). The same thing always happens to me:

    So I continue thinking that is because the strange code of above.
     
  5. unity_bill

    unity_bill

    Unity Technologies

    Joined:
    Apr 11, 2017
    Posts:
    954
    Sounds like we have a couple issues all wrapped up into one post. For one, I think we should always return a valid op, even if the op is done or not doing anything (to prevent nullRef's). So that's a bug.

    Second, in your sample project everything is set up to be local (loading from streaming assets). As such, GetDownloadSize will return 0, and DownloadDependencies should finish immediately with nothing to download.
     
  6. MaskedMouse

    MaskedMouse

    Joined:
    Jul 8, 2014
    Posts:
    389
    Ah my bad. It seems I've been a bit too quick in setting up a minimal build.
    I've set them to remote and hooked up a local hosting service. It does show the size.
    Just when clicking yes to actually download them it throws a null reference for the task.
    So yeah GetDownloadSize works indeed. Wondering what went wrong before in my bigger project. I will have to debug that later on, maybe the same problem that I forgot to put them remote. If I end up finding another bug I'll let you know.
     
    unity_bill likes this.