Search Unity

Invalid Key exception despite being correct

Discussion in 'Addressables' started by crawfis, Jan 18, 2022.

  1. crawfis

    crawfis

    Joined:
    Jan 30, 2014
    Posts:
    114
    I get the following error:
    Code (CSharp):
    1. UnityEngine.AddressableAssets.InvalidKeyException: Exception of type 'UnityEngine.AddressableAssets.InvalidKeyException' was thrown., Key=Assets/TileSets/Dungeons/DungeonRoomTileSet/DungeonRoom_0000.prefab, Type=UnityEngine.GameObject
    2. UnityEngine.AddressableAssets.Addressables:LoadAssetsAsync<UnityEngine.GameObject> (object,System.Action`1<UnityEngine.GameObject>)
    3. CrawfisSoftware.Tiling.Unity.UnityTileSetBuilder:CreateTileSet () (at Assets/Scripts/TilingScripts/UnityTileSetBuilder.cs:55)
    4. CreateMultiTiling:Build (UnityEngine.Transform) (at Assets/Scripts/TilingScripts/CreateMultiTiling.cs:44)
    5. TilingScripts.MultiTilingEditor:OnInspectorGUI () (at Assets/Scripts/TilingScripts/Editor/MultiTilingEditor.cs:24)
    6. UnityEngine.GUIUtility:ProcessEvent (int,intptr,bool&)
    7.  
    I have tried a Build->Clean Build All, followed by a Build->New Build->Default Build Script and it still will not see this. Here is a screenshot of the Addressables Group which clearly has the correct "Key". UnityAddressables.png

    I use this code:
    Code (CSharp):
    1. Addressables.LoadAssetsAsync<GameObject>(new List<object> { assetName }, null).Completed += (asset) => ...
    2.  
    where:
    Code (CSharp):
    1. string assetName = "Assets/TileSets/Dungeons/DungeonRoomTileSet/DungeonRoom_0000.prefab";
    2.  
    Looked at it 20 times and the names match. Any idea what is going on? Bug?

    Running on Unity 2021.2.1f1 with Addressables 1.19.17
     

    Attached Files:

  2. abogarsukov-braingames

    abogarsukov-braingames

    Joined:
    Jul 23, 2020
    Posts:
    40
    This error can be caused by long names. Try right-click the addressable item and 'Simplify Addressable Name'.
     
    davidla_unity likes this.
  3. crawfis

    crawfis

    Joined:
    Jan 30, 2014
    Posts:
    114
    @abogarsukov-braingames I tried that. I also tried deleting the Library and rebuilding. I should also add that this used to work. I think upgrading in Unity and Addressables has introduced a bug.
     
    Last edited: Jan 20, 2022
  4. weiping-toh

    weiping-toh

    Joined:
    Sep 8, 2015
    Posts:
    192
    Seems like a problem with the catalog. Invalid Key Exception is usually an indication to the catalog being incorrect.
    Are you using multiple catalogs(content updating)?
     
  5. crawfis

    crawfis

    Joined:
    Jan 30, 2014
    Posts:
    114
    I only have one catalog. I have several groups though. I can see the assets in the Catalog with the simple line:
    "Assets/TileSets/Dungeons/DungeonRoomTileSet/DungeonRoom_0000.prefab",
     
  6. Peter77

    Peter77

    QA Jesus

    Joined:
    Jun 12, 2013
    Posts:
    6,609
    Wonderful, I'm seeing this issue sometimes as well. I thought I was crazy, because the key actually did match with what's in the Addressables Group window. I'm relieved I'm not alone with this issue.

    I'm unable to reproduce the issue though. It seems to occur randomly and also not for everyone. I saw it in Unity Cloud Diagnostics for a few players only, most players don't run into this problem.

    Are you able to reproduce it? If you're able to reproduce the issue, can you submit a bug-report to Unity Technologies? Otherwise I'm afraid they're not able to fix it.

    I'm using Unity 2019.4.20f1 and Addressables 1.19.9 btw.
     
    User414322 likes this.
  7. davidla_unity

    davidla_unity

    Unity Technologies

    Joined:
    Nov 17, 2016
    Posts:
    763
    So, the issue is a weirdness with the API itself. What you're passing in, the system thinks that List is the key, it doesn't recognize it as a list of individual keys. So, you'll need to change the API to actually use the overload with the MergeMode, and change the List to be typed <string> (I think List<object> with the MergeMode is deprecated, I assume it has something to do with the key not getting recognized correctly again). The MergeMode overloads are able to correctly recognize the object sent in and use Lists like you'd expect.

    tl;dr
    Change
    Code (CSharp):
    1. Addressables.LoadAssetsAsync<GameObject>(new List<object> { assetName }, null).Completed += (asset) =>
    to
    Code (CSharp):
    1. Addressables.LoadAssetsAsync<GameObject>(new List<string> { assetName }, null, Addressables.MergeMode.None).Completed += (asset) =>
    and you should be good to go.
     
  8. crawfis

    crawfis

    Joined:
    Jan 30, 2014
    Posts:
    114
    Thanks @davidla_unity. Yes. I realized I was being a little dumb. The extra "s" in the call required a list. The better alternative is to use LoadAssetAsync. Once I has that plural I started running into all sorts of problems. Now if I can just figure out how to get my Editor tools to work with Addressables (and not hand the GUI thread) I will be all set. Any resources on using Addressables (Preload only once) with Editor tools I would appreciate any pointers.
     
  9. better_walk_away

    better_walk_away

    Joined:
    Jul 12, 2016
    Posts:
    291
    I am using Unity 2019.4.22f1 and Addressables 1.16.19. We are experiencing the same issue. Some players throw InvalidKeyException despite the key being correct, while some people don’t.
    In my case, some players failed to load a prefab in a local bundle, so I was not loading a list of assets nor downloading, there was no network issue at play.
    I also did not know the reason, nor could I reproduce the issue myself, unfortunately.
     
    Last edited: Jul 27, 2022