Search Unity

'UnityEngine.AddressableAssets.InvalidKeyException' was thrown

Discussion in 'Addressables' started by MSFX, Dec 19, 2019.

  1. MSFX

    MSFX

    Joined:
    Sep 3, 2009
    Posts:
    115
    I'm testing our addressable system and want to deal with error handling nicely obviously for when a key doesn't exist should a typo creep in somewhere but it seems that the system throws this exception before I get a chance to deal with it...? Even wrapping in try/catch it never catches where I'm expecting it to...?

    Code (CSharp):
    1.  
    2.     async void Start() {
    3.         try {
    4.             var loader = Addressables.LoadAssetAsync<Sprite>("invalid key");
    5.             await loader.Task;
    6.         } catch (System.Exception ex) {
    7.             Debug.Log("Why don't we get this? " + ex.Message);
    8.         }
    9.     }
    This results in two exceptions...

    Code (CSharp):
    1. Exception encountered in operation UnityEngine.ResourceManagement.ResourceManager+CompletedOperation`1[UnityEngine.Sprite], result='', status='Failed': Exception of type 'UnityEngine.AddressableAssets.InvalidKeyException' was thrown., Key=invalid key
    2.  
    3. Exception encountered in operation UnityEngine.AddressableAssets.Initialization.InitializationOperation, result='', status='Succeeded' - Chain<Sprite>: ChainOperation of Type: UnityEngine.Sprite failed because dependent operation failed
    4. Exception of type 'UnityEngine.AddressableAssets.InvalidKeyException' was thrown., Key=invalid key
    I've found one other thread from months ago mentioning this but how is no-one else having this issue?? Is there some additional way to check the asset exists before attempting to load that I'm missing?
     
  2. KB73

    KB73

    Joined:
    Feb 7, 2013
    Posts:
    218
    We just trawled through Addressables.ResourceLocators and cached off the asset once loaded, we do a check again our handle to see if it is loaded or not

    Probably a better way but this worked for us
     
  3. faolad

    faolad

    Joined:
    Jan 27, 2013
    Posts:
    98
    I need a solution for this too. Want to deal with scene loading errors
     
  4. MSFX

    MSFX

    Joined:
    Sep 3, 2009
    Posts:
    115
    So I've managed to work out this solution but it feels way too verbose that it needs to be....

    Code (CSharp):
    1.  
    2. var validateAddress = Addressables.LoadResourceLocationsAsync(address);
    3. await validateAddress.Task;
    4. if (validateAddress.Status == UnityEngine.ResourceManagement.AsyncOperations.AsyncOperationStatus.Succeeded) {
    5.         if (validateAddress.Result.Count > 0) {
    6.                 // asset exists go ahead and load
    7.         }
    8. }
    .
     
  5. chanon81

    chanon81

    Joined:
    Oct 6, 2015
    Posts:
    144
unityunity