Search Unity

'UnityEngine.AddressableAssets.InvalidKeyException' was thrown

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

  1. MSFX

    MSFX

    Joined:
    Sep 3, 2009
    Posts:
    116
    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?
     
    Tymianek and C0b0ll like this.
  2. KB73

    KB73

    Joined:
    Feb 7, 2013
    Posts:
    234
    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
     
    C0b0ll likes this.
  3. faolad

    faolad

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

    MSFX

    Joined:
    Sep 3, 2009
    Posts:
    116
    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. }
    .
     
    PragneshRathod likes this.
  5. chanon81

    chanon81

    Joined:
    Oct 6, 2015
    Posts:
    168
  6. mitaywalle

    mitaywalle

    Joined:
    Jul 1, 2013
    Posts:
    253
    Please, help. Get InvalidKeyException, use default assets for settings. Analyze tells no errors, use Default Build Script, addressable is under packed Assets group.

    http://prntscr.com/t3hohh

    Code for load / Instantiate:

    Code (CSharp):
    1.         var validateKeyAsync = Addressables.LoadResourceLocationsAsync(key);
    2.         validateKeyAsync.Completed += validateAsync =>
    3.         {
    4.             if (validateAsync.Status != AsyncOperationStatus.Succeeded)
    5.             {
    6.                 Debug.LogError($"[ Spawn Failed ] '{key}' invalid!");
    7.                 return;
    8.             }
    9.  
    10.             if (validateAsync.Result.Count <= 0)
    11.             {
    12.                 Debug.LogError($"[ Spawn Failed ] address '{key}' results.Count == 0!");
    13.                 return;
    14.             }
    15.  
    16.             Addressables.LoadAssetAsync<GameObject>(key).Completed += laodAsync =>
    17.             {
    18.                 if (laodAsync.Status != AsyncOperationStatus.Succeeded || !laodAsync.Result) return;
    19.  
    20.                 Addressables.InstantiateAsync(TextUtility.AddressById(data.id),
    21.                     new InstantiationParameters(pos, Quaternion.identity, null)).Completed += instantiateAsync =>
    22.                 {
    23.                     if (instantiateAsync.Status != AsyncOperationStatus.Succeeded) return;
    24.  
    25.                     var go = instantiateAsync.Result;
    26.                     var item = go.GetComponent<Item>();
    27.                     item.data = data;
    28.                     item.transform.position = pos;
    29.                 };
    30.             };
    31.         };

    validateAsync.Result.Count is always 0. If I comment this check out, I get InvalidKeyException. Prefab Is simple cube (ItemScript, Rigidbody, Collider, MeshRederer)

    Works only in AssetDatabase mode. Simulate groups / use existing modes not work
     
    Last edited: Jun 20, 2020
  7. BlockFade

    BlockFade

    Joined:
    Jan 28, 2017
    Posts:
    25
    I think you forgot to Build your Addressable Database, open the Addressables window and go to Build in the top right, then click New Build.
     
  8. PixelFireXY

    PixelFireXY

    Joined:
    Nov 2, 2011
    Posts:
    56
    Thank you so much! This solved my problem!
     
  9. virgilcwyile

    virgilcwyile

    Joined:
    Jan 31, 2016
    Posts:
    73
    Solved my problem too. Had to New Build it. Thanks guys