How would I add code to manage when a key is not found rather than throwing an error? (e.g. i cant figure out how and how to put in a try / catch with addressables?)
Example method below
**
Exception encountered in operation CompletedOperation, status=Failed, result= : Exception of type ‘UnityEngine.AddressableAssets.InvalidKeyException’ was thrown., Key=Occupations,
If there is no key found an error occurs when loading an addressable asset
public IList<GameObject> SpawnAssets(string key, int num, Transform spawnPos, IList<GameObject> list)
{
Addressables.LoadAssetAsync<GameObject>(key).Completed += async asset =>
{
if (asset.Status == AsyncOperationStatus.Failed)
{
Debug.Log($"Key failed to load: {key}");
return;
}
if (asset.Status == AsyncOperationStatus.Succeeded)
{
for (int i = 0; i < num; i++)
{
GameObject go = asset.Result;
go = Instantiate(go, go.transform.position, go.transform.rotation);
go.transform.SetParent(spawnPos, false);
list.Add(go);
go.name = $"Button{i}";
}
}
};
return list;
}