Unity-Technologies/UnityCsReference · error · ArgumentNullException

clips

Error message

clips

What it means

ArgumentNullException thrown by AudioRandomContainerExtensions.AddElements(AudioClip[] clips) when the clips array is null. This is a standard null-argument guard ensuring the clips parameter is a valid array before accessing its Length property.

Source

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

        ValidateContainer(container);

        if (count <= 0)
            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));

View on GitHub (pinned to 225b0fbdb5)

Solutions

  1. Initialize the clips array before passing it, or check for null before calling AddElements.
  2. If clips may legitimately be absent, guard the call: if (clips != null) container.AddElements(clips);
  3. Ensure SerializedProperty.arraySize > 0 before extracting AudioClip elements into an array.
  4. Use Array.Empty<AudioClip>() as a default instead of null, then let the empty-check guide behavior.

Example fix

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

Strategy: type-guard

Validate before calling

// Null-check clips before calling AddElements
if (clips != null)
    container.AddElements(clips);

Type guard

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

Prevention

When it happens

Trigger: Calling container.AddElements(null) or passing a variable that was never assigned. Occurs when a clips array field is uninitialized, a serialized property resolves to null, or a code path skips array initialization before calling AddElements.

Common situations: Editor scripts that read an AudioClip[] from a SerializedProperty that is unassigned, drag-and-drop handlers where no clips were dropped, deserialized data with missing clip arrays, conditional code paths that leave the array null.

Related errors


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