'UnityEngine.AddressableAssets.InvalidKeyException' was thrown

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…?

    async void Start() {
        try {
            var loader = Addressables.LoadAssetAsync<Sprite>("invalid key");
            await loader.Task;
        } catch (System.Exception ex) {
            Debug.Log("Why don't we get this? " + ex.Message);
        }
    }

This results in two exceptions…

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

Exception encountered in operation UnityEngine.AddressableAssets.Initialization.InitializationOperation, result='', status='Succeeded' - Chain<Sprite>: ChainOperation of Type: UnityEngine.Sprite failed because dependent operation failed
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 Likes

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

1 Like

I need a solution for this too. Want to deal with scene loading errors

So I’ve managed to work out this solution but it feels way too verbose that it needs to be…

var validateAddress = Addressables.LoadResourceLocationsAsync(address);
await validateAddress.Task;
if (validateAddress.Status == UnityEngine.ResourceManagement.AsyncOperations.AsyncOperationStatus.Succeeded) {
        if (validateAddress.Result.Count > 0) {
                // asset exists go ahead and load
        }
}

.

1 Like

I’m not sure, but this might be useful for you:

It allows you to check if an asset key is valid synchronously … which the Addressable devs seem to think is blasphemous.

Please, help. Get InvalidKeyException, use default assets for settings. Analyze tells no errors, use Default Build Script, addressable is under packed Assets group.

code

Code for load / Instantiate:

        var validateKeyAsync = Addressables.LoadResourceLocationsAsync(key);
        validateKeyAsync.Completed += validateAsync =>
        {
            if (validateAsync.Status != AsyncOperationStatus.Succeeded)
            {
                Debug.LogError($"[ Spawn Failed ] '{key}' invalid!");
                return;
            }

            if (validateAsync.Result.Count <= 0)
            {
                Debug.LogError($"[ Spawn Failed ] address '{key}' results.Count == 0!");
                return;
            }

            Addressables.LoadAssetAsync<GameObject>(key).Completed += laodAsync =>
            {
                if (laodAsync.Status != AsyncOperationStatus.Succeeded || !laodAsync.Result) return;

                Addressables.InstantiateAsync(TextUtility.AddressById(data.id),
                    new InstantiationParameters(pos, Quaternion.identity, null)).Completed += instantiateAsync =>
                {
                    if (instantiateAsync.Status != AsyncOperationStatus.Succeeded) return;

                    var go = instantiateAsync.Result;
                    var item = go.GetComponent<Item>();
                    item.data = data;
                    item.transform.position = pos;
                };
            };
        };

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

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.

11 Likes

Thank you so much! This solved my problem!

Solved my problem too. Had to New Build it. Thanks guys

Is there a reason why I have to do this:

        private IEnumerator DoLoadAudio(string langID)
        {
            /* This block is stupid, but necessary or line 12 will throw an uncatchable error. */
            AsyncOperationHandle<IList<IResourceLocation>> validateAddress = Addressables.LoadResourceLocationsAsync(langID);
            yield return validateAddress.Task;
            if (validateAddress.Status != AsyncOperationStatus.Succeeded || validateAddress.Result == null || validateAddress.Result.Count == 0)
            {
                yield break;
            }

            List<string> keys = new List<string>() { langID };
            loadHandle = Addressables.LoadAssetsAsync<AudioClip>(keys, null, Addressables.MergeMode.Union, false);
          
            loadHandle.Completed += operation =>
            {
                foreach (AudioClip clip in operation.Result)
                {
                    // ...
                }
            };

            yield return loadHandle;
        }

And oh yeah the validateTask returns “success” with a count of 0 in the situation where the loadHandle errors.