Getting InvalidKeyException but Key shows on Addressables.ResourceLocators

Wow, thank you so much @ProtoTerminator for this answer on an unrelated issue: Difference between InstantiateAsync and LoadAssetAsync

It looks like the error is completely misleading. The problem was not that it was not finding the key, but the type was wrong.

This is the original code I was trying to port:

Enemy enemy = Instantiate(enemyPrefab, enemiesContainer.transform.position, enemiesContainer.transform.rotation);```

And this is what I was trying (which led to the error in the first post): 

```Enemy enemyPrefab = await Addressables.LoadAssetAsync<Enemy>(enemyType.toStringValue()).Task;
Enemy enemy = Instantiate(enemyPrefab, enemiesContainer.transform.position, enemiesContainer.transform.rotation);```

Now I changed it to this:

```GameObject enemyPrefab = await Addressables.LoadAssetAsync<GameObject>(enemyType.toStringValue()).Task;
Enemy enemy = Instantiate(enemyPrefab, enemiesContainer.transform.position, enemiesContainer.transform.rotation).GetComponent<Enemy>();```

And everything is working fine now. By the way, if there is a better way to do this, please let me know.

Changing the post tag to Bug in hopes the exception can be fixed to better address the issue.
2 Likes