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

NullReferenceException when using Addressables.DownloadDependenciesAsync(label)

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

  1. MaskedMouse

    MaskedMouse

    Joined:
    Jul 8, 2014
    Posts:
    482
    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:
    98
    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:
    1,002
    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:
    482
    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.
  7. DrunkenMastah

    DrunkenMastah

    Joined:
    Sep 26, 2017
    Posts:
    29
    @unity_bill Any update on this? I just updated to using Addressables only to find that I cannot download any assets due to a null reference error on Addressables.DownloadDependenciesAsync(<InsertLabeNameHere>).
    1. Any workaround until there is a fix?
    2. Is there any example code that does show the right way for pre-downloading all remote assets using Addressables? Like how asset bundles are downloaded at loading time? (I couldn't find any examples in either basic or advanced examples. And the documentation does not have any example either)
    3. Requesting an updated link to addressables documentation for the latest version.
     
    Last edited: Oct 24, 2019
  8. unity_bill

    unity_bill

    Unity Technologies

    Joined:
    Apr 11, 2017
    Posts:
    1,002
    Is it still broken? I feel like we fixed this, but being a months old post, I might be mis-remembering. If you are still seeing it, please outline the repro. 2. not sure exactly what you need.
    3. https://docs.unity3d.com/Packages/com.unity.addressables@1.2/manual/index.html but the best way to get the latest docs is to either watch the "new release" thread on this forum, or click "view docs" from within package manager.
     
  9. DrunkenMastah

    DrunkenMastah

    Joined:
    Sep 26, 2017
    Posts:
    29

    Regarding point 2:
    https://docs.unity3d.com/Packages/c...2/manual/AddressableAssetsGettingStarted.html
    Downloading in Advance

    Calling the Addressables.DownloadDependenciesAsync() method loads the dependencies for the address or label that you pass in. Typically, this is the asset bundle.

    There is no example project of this case anywhere that I could find.

    Addressables.DownloadDependenciesAsync("spaceHazards");
    Here if "spaceHazaards" is a resource label (and has been applied to a bunch of assets).
    I get exception at runtime: "Key is invalid exception".

    I really just want to see one sample project that actually does this. It would be great, if you can send me a link to a sample project that uses this technique for a remote resource that is NOT using Unity editor for hosting.

    I am currently doing a wierd way to getting my asset bundles downloaded by making a call to a known asset reference that is part of my Remote Group
    Addressables.DownloadDependenciesAsync("known_asset_reference_in_remote_group");
    And it works. But i wish I could pass in the resource label instead.

    4:
    After doing the above I do get the download to work from a remote resource (azure CDN), but I get this error on device.
    Unity 2019.2.10f1
    It already has Strip Engine code disabled. This is a blocker for me now.

    Code (csharp):
    1.  
    2. Could not produce class with ID 1108.
    3. This could be caused by a class being stripped from the build even though it is needed. Try disabling 'Strip Engine Code' in Player Settings.
    4. ..
    5. ..
    6. (Filename: ./Runtime/Serialize/PersistentManager.cpp Line: 1543)
     
    Last edited: Oct 25, 2019
    qfhuang likes this.
  10. unity_bill

    unity_bill

    Unity Technologies

    Joined:
    Apr 11, 2017
    Posts:
    1,002
    Calling DownloadDependenciesAsync or LoadAssetsAsync should either both work (If the catalog knows about the key/label/whatever) or both not work (if the catalog does not know about it).

    So please add a little more detail...
    1. Download fails, but Load works - this would be really weird. if this is the case, definitely open a bug against unity with a repro.
    2. both download and load fail - the catalog doesn't have your key. Likely you either have a typo in the label (note it's case sensitive) or your label isn't on anything as of the last build.


    For the code stripping issue, this is likely a bug that will require a repro project and an official ticket against Unity. That being said, it would probably be best to resolve your invalid key issue first, and download through our system. I don't know how these could be related, but if you are reporting a bug that's built upon a workaround, it's a lot harder to track down.
     
  11. DrunkenMastah

    DrunkenMastah

    Joined:
    Sep 26, 2017
    Posts:
    29
    I used the code in this post and made a similar one for my needs.
    https://forum.unity.com/threads/awa...etasync-exceptions-not-bubbling-1-1-7.723401/
    which means that this bug is still present in latest version as well.

    Code (CSharp):
    1. yield return Addressables.DownloadDependenciesAsync(remoteResourcesLabel).WaitForAsyncOperationToComplete();
    Along with this extension
    Code (CSharp):
    1. public static IEnumerator WaitForAsyncOperationToComplete(this AsyncOperationHandle handle)
    2.         {
    3.             while (!handle.IsDone)
    4.             {
    5.                 yield return null;
    6.             }
    7.  
    8.             Debug.Log($"Result = {handle.Result}");
    9.         }
    10.         public static IEnumerator WaitForAsyncOperationToComplete<T>(this AsyncOperationHandle<T> handle)
    11.         {
    12.             while (!handle.IsDone)
    13.             {
    14.                 yield return null;
    15.             }
    16.  
    17.             Debug.Log($"Result = {handle.Result}");
    18.         }
    19.  

    works for me. It seems to download now.
    But, when all these days when i was doing
    Code (CSharp):
    1. await  Addressables.DownloadDependenciesAsync(remoteResourcesLabel).Task;
    it did not work as in it threw an exception.
    Not sure why this should not work.

    I think the best way for me to figure out and produce a repro is to have a single source of truth from an official example instead of me trying to make this work.
    Everything is easier with an example.

    Most likely its me trying to use the system in a way that you don't want me to :)
    So I request you to provide an example project that does "Remote Download" which uses DownloadDependenciesAsync method.


    Regarding point 4: I found that I still get the errors on device but it does not stop me from loading into the game. So not sure how serious that bug is. Maybe i will only know what it broke after i do a thorough testing. So for now. I am able to load into the game good.
    (btw, loving this new system. Wishing you guys make a whole lot of sample projects to cover all possible cases)
     
    Last edited: Nov 2, 2019
    qfhuang likes this.
  12. DrunkenMastah

    DrunkenMastah

    Joined:
    Sep 26, 2017
    Posts:
    29
    Found the main reason why DownloadDependenciesAsync function or many other functions related to Addressables did not work and were flaky..
    Reason: The "Clear Cache button" is broken. Yes even in the 1.4.0 version.
    To fix all my issues I now manually delete 2 sections everytime i build bundles.
    1. Delete /Library/com.addressables.<gamename>
    2. %APPDATA%/locallow/<company name>/<gamename>/com.addressables.<gamename>

    Deleting these 2 folder fixes all the problems for me.
     
  13. newatlanta19006

    newatlanta19006

    Joined:
    Feb 23, 2019
    Posts:
    7
    Any update on the Task being null in DownloadDependenciesAsync? Still broken in 1.6 it seems.
     
    qfhuang likes this.
  14. VaygrEmpire

    VaygrEmpire

    Joined:
    Sep 30, 2016
    Posts:
    40
    Hmm, I used DownloadDependenciesAsync() yesterday and it worked fine. Though I didn't know it can take Label name....I passed AssetReference in it (So something like Addressables.DownloadDependenciesAsync(EnemyAssetReference) and it loaded wonderfully. I'm using 1.10
     
    Last edited: Feb 6, 2020
  15. qfhuang

    qfhuang

    Joined:
    Dec 5, 2019
    Posts:
    1
    For what I found, it's just because async/await not working on
    DownloadDependenciesAsync
    :

    WRONG:
    Code (CSharp):
    1. await Addressables.DownloadDependenciesAsync(<Label>).Task;
    CORRECT:
    Code (CSharp):
    1. Addressables.DownloadDependenciesAsync(<Label>).Completed += (op) =>
    2. {
    3.     Debug.Log(op.Status);
    4. }
    Addressable version: 1.6.0
     
    TigerHix likes this.
  16. araki_yuta

    araki_yuta

    Joined:
    Jan 21, 2020
    Posts:
    14
    you can also do:
    Code (CSharp):
    1.  
    2. public Task DownloadDependenciesTask(<Label>) {
    3.     var handle = Addressables.DownloadDependenciesAsync(<Label>);
    4.     var completionSource = new TaskCompletionSource<object>();
    5.     handle.Completed += op =>
    6.     {
    7.         if (op.Status == AsyncOperationStatus.Succeeded)
    8.         {
    9.              completionSource.TrySetResult(handle.Result);        
    10.         };
    11.     }
    12.     return completionSource.Task;    
    13. }
    14.  
    then
    await DownloadDependenciesTask(<Label>);
     
  17. LeoThurler

    LeoThurler

    Joined:
    Jan 5, 2016
    Posts:
    1
    Hi @unity_bill,

    Do you also have a fix estimate? I tested it today (version: 1.7.4) and still broken.

     
    Last edited: Mar 19, 2020
  18. jwtan_unity

    jwtan_unity

    Unity Technologies

    Joined:
    Nov 28, 2018
    Posts:
    8
    Here is the bug report on the issue tracker. Please vote on it if this issue is affecting you.
     
  19. ThundThund

    ThundThund

    Joined:
    Feb 7, 2017
    Posts:
    8
    Voted. This a quite important bug that has been there for almost a year. Is there any news? @unity_bill
     
  20. ThundThund

    ThundThund

    Joined:
    Feb 7, 2017
    Posts:
    8
    Version 1.8.3, by the way.
     
  21. EdwinLyons

    EdwinLyons

    Joined:
    Oct 4, 2012
    Posts:
    55
    Also voted on this - quite a pain and has wasted a few hours of my time before I found this thread.
     
  22. retypeNickEA

    retypeNickEA

    Joined:
    Oct 3, 2017
    Posts:
    3
    Would be nice to be fixed, without popularity voting. Quite essential method in Addressable API
     
  23. DrunkenMastah

    DrunkenMastah

    Joined:
    Sep 26, 2017
    Posts:
    29
    Paying 50$ a month and yet begging for bug fixes..this is what it has come to..
     
unityunity