Skip to content

Addressables.InstantiateAsync().ToUniTask() causes InvalidKeyException with bundle dependencies #694

@utkusaridede

Description

@utkusaridede

Environment
Unity Version: 2022.3.62f2
UniTask Version: 2.5.10
Addressables Version: 1.21.21
Platform: Android / iOS / Unity Editor (Use Existing Build Mode)

Description
When converting Addressables.InstantiateAsync from the IEnumerator pattern to UniTask using .ToUniTask(), the operation fails with bundle loading errors. The same code works correctly when using the IEnumerator pattern with yield return.

Reproduction Steps:

❌ FAILS - UniTask Pattern:

private async UniTaskVoid InstantiateTransitionPrefab()
{
    var asyncHandle = Addressables.InstantiateAsync(
        transitionAddress,
        Vector3.zero,
        Quaternion.identity,
        ParentCanvas.transform
    );
    
    await asyncHandle.ToUniTask();  // ❌ Fails here
    
    if (asyncHandle.Status == AsyncOperationStatus.Succeeded)
    {
        transitionInstance = asyncHandle.Result;
    }
}

Error Message:
InvalidKeyException: Exception of type 'UnityEngine.AddressableAssets.InvalidKeyException' was thrown.
Unable to load dependent bundle from location [bundle_name]

✅ WORKS - IEnumerator Pattern:

private IEnumerator InstantiateTransitionPrefab()
{
    var asyncHandle = Addressables.InstantiateAsync(
        transitionAddress,
        Vector3.zero,
        Quaternion.identity,
        ParentCanvas.transform
    );
    
    yield return asyncHandle;  // ✅ Works correctly
    
    if (asyncHandle.Status == AsyncOperationStatus.Succeeded)
    {
        transitionInstance = asyncHandle.Result;
    }
}

Analysis:
The issue appears to be related to how .ToUniTask() awaits the AsyncOperationHandle compared to Unity's native yield return with IEnumerator:
IEnumerator pattern: Unity's coroutine system properly waits for all dependency bundles to load before completing
ToUniTask() pattern: The conversion to UniTask appears to complete the await before all dependencies are fully loaded, causing the InvalidKeyException

This only affects InstantiateAsync - other Addressables methods work fine with .ToUniTask():.
✅ LoadAssetAsync().ToUniTask() - Works
✅ DownloadDependenciesAsync().ToUniTask() - Works
✅ LoadSceneAsync().ToUniTask() - Works
❌ InstantiateAsync().ToUniTask() - Fails

Current Workaround:
We've reverted InstantiateAsync calls to use the IEnumerator pattern while keeping other Addressables operations with UniTask.

Expected Behavior:
Addressables.InstantiateAsync().ToUniTask() should wait for all dependent bundles to load completely before returning, matching the behavior of yield return asyncHandle.

Actual Behavior:
The operation completes prematurely, causing dependent bundles to fail loading with InvalidKeyException.

Questions:
Is this a known limitation of InstantiateAsync with UniTask?
Is there a different pattern we should use for instantiating addressable prefabs with UniTask?
Could this be related to timing differences in how UniTask's .ToUniTask() extension handles AsyncOperationHandle completion?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions