Cysharp/UniTask · error · ArgumentException

The tasks argument contains no tasks.

Error message

The tasks argument contains no tasks.

What it means

Thrown by WhenAnyPromise<T> constructor (the generic WhenAny for UniTask<T>) when tasksLength is zero. WhenAny semantically requires at least one task to race; with zero tasks there is nothing to await and the result is undefined. UniTask rejects this at construction rather than hanging forever.

Source

Thrown at src/UniTask/Assets/Plugins/UniTask/Runtime/UniTask.WhenAny.cs:186

            }

            void IUniTaskSource.GetResult(short token)
            {
                GetResult(token);
            }
        }


        sealed class WhenAnyPromise<T> : IUniTaskSource<(int, T)>
        {
            int completedCount;
            UniTaskCompletionSourceCore<(int, T)> core;

            public WhenAnyPromise(UniTask<T>[] tasks, int tasksLength)
            {
                if (tasksLength == 0)
                {
                    throw new ArgumentException("The tasks argument contains no tasks.");
                }

                TaskTracker.TrackActiveTask(this, 3);

                for (int i = 0; i < tasksLength; i++)
                {
                    UniTask<T>.Awaiter awaiter;
                    try
                    {
                        awaiter = tasks[i].GetAwaiter();
                    }
                    catch (Exception ex)
                    {
                        core.TrySetException(ex);
                        continue; // consume others.
                    }

                    if (awaiter.IsCompleted)

View on GitHub (pinned to ceac8d6946)

Solutions

  1. Check that the task collection is non-empty before calling WhenAny
  2. Fall back to a default task or UniTask.Never when the list is empty
  3. Guard with: if (tasks.Count == 0) return; before WhenAny

Example fix

// before
var (winIndex, _) = await UniTask.WhenAny(tasks.ToArray());

// after
if (tasks.Count == 0) return;
var (winIndex, _) = await UniTask.WhenAny(tasks.ToArray());
Defensive patterns

Strategy: validation

Validate before calling

if (tasks == null || tasks.Length == 0)
{
    // handle empty case: return default, await Never, or skip
    return default;
}
var (winIndex, result) = await UniTask.WhenAny(tasks);

Type guard

static bool HasAnyTasks<T>(UniTask<T>[] tasks) => tasks != null && tasks.Length > 0;

Prevention

When it happens

Trigger: Calling UniTask.WhenAny(emptyArray) or WhenAny(list.ToArray()) on an empty collection. Passing a tasks array whose effective length (tasksLength) is zero.

Common situations: Dynamically building a task list that ends up empty due to filtering or all items being skipped. A race between populating a list and calling WhenAny. Editor or test scenarios with no registered tasks.

Related errors


AI-assisted analysis of Cysharp/UniTask@ceac8d6946 (2026-08-13). Data as JSON: /api/errors/8d0b6520c2406eac. Report an issue: GitHub.