Unity-Technologies/UnityCsReference · error · ArgumentException

Must not be empty.

Error message

Must not be empty.

What it means

ArgumentException thrown by AudioRandomContainerExtensions.AddElements(AudioClip[] clips) when the clips array has zero elements. The guard prevents adding zero elements (a no-op that would still create an undo group), ensuring the operation is meaningful.

Source

Thrown at Editor/Mono/Audio/AudioRandomContainerExtensions.cs:43

            throw new ArgumentOutOfRangeException(nameof(count), "Must be greater than zero.");

        AddElementsInner(container, count, (i, element) => { });
    }

    /// <summary>
    /// Adds a given number of new <see cref="AudioContainerElement"/> objects with clips assigned to <see cref="AudioRandomContainer.elements"/>.
    /// </summary>
    /// <param name="container">The instance to add the elements to.</param>
    /// <param name="clips">An array of <see cref="AudioClip"/> objects to be assigned to each new element.</param>
    internal static void AddElements(this AudioRandomContainer container, AudioClip[] clips)
    {
        ValidateContainer(container);

        if (clips == null)
            throw new ArgumentNullException(nameof(clips));

        if (clips.Length == 0)
            throw new ArgumentException("Must not be empty.", nameof(clips));

        AddElementsInner(container, clips.Length, (i, element) =>
        {
            if (clips[i] != null)
                element.audioClip = clips[i];
        });
    }

    static void ValidateContainer(AudioRandomContainer container)
    {
        if (container == null)
            throw new ArgumentNullException(nameof(container));

        if (!EditorUtility.IsPersistent(container))
            throw new ArgumentException("Must be a persistent asset.", nameof(container));
    }

    static void AddElementsInner(AudioRandomContainer container, int count,

View on GitHub (pinned to 225b0fbdb5)

Solutions

  1. Check clips.Length > 0 before calling AddElements.
  2. Guard the call site: if (clips != null && clips.Length > 0) container.AddElements(clips);
  3. If filtering clips, skip the AddElements call entirely when the filtered result is empty.
  4. Validate user selection contains at least one AudioClip before proceeding.

Example fix

// before
container.AddElements(selectedClips); // may be empty
// after
if (selectedClips != null && selectedClips.Length > 0)
    container.AddElements(selectedClips);
Defensive patterns

Strategy: validation

Validate before calling

// Check for non-empty clips before calling AddElements
if (clips != null && clips.Length > 0)
    container.AddElements(clips);

Type guard

static bool HasClips(AudioClip[] clips) => clips != null && clips.Length > 0;

Prevention

When it happens

Trigger: Calling container.AddElements(new AudioClip[0]) or passing an array whose Length is 0. Occurs when filtering operations remove all clips, when a selection contains no audio clips, or when a UI operation results in an empty clips list.

Common situations: Drag-and-drop handlers where only non-AudioClip assets were dropped, filtered lists where no clips match the filter criteria, SerializedProperty arrays with arraySize 0, batch operations on empty selections.

Related errors


AI-assisted analysis of Unity-Technologies/UnityCsReference@225b0fbdb5 (2026-08-13). Data as JSON: /api/errors/404d85e0dd8ab420. Report an issue: GitHub.