Unity-Technologies/UnityCsReference · error · ArgumentException

length has to be longer than zero

Error message

length has to be longer than zero

What it means

ArgumentException thrown by the ClipPreviewDetails constructor when localLength is zero or negative. localLength defines the duration (in normalized clip time) of the audio segment to preview; a non-positive value would produce an empty or invalid waveform render.

Source

Thrown at Editor/Mono/Audio/StreamedAudioClipPreview.cs:58

        {
            public float[] preview;
            public int previewSamples;
            public double normalizedDuration;
            public double normalizedStart;
            public double deltaStep;
            public AudioClip clip;
            public int previewPixelsToRender;
            public double localStart;
            public double localLength;
            public bool looping;

            public ClipPreviewDetails(AudioClip clip, bool isLooping, int size, double localStart, double localLength)
            {
                if (size < 2)
                    throw new ArgumentException("Size has to be larger than 1");

                if (localLength <= 0)
                    throw new ArgumentException("length has to be longer than zero", "localLength");

                if (localStart < 0)
                    throw new ArgumentException("localStart has to be positive", "localStart");

                if (clip == null)
                    throw new ArgumentNullException("clip");

                this.clip = clip;

                preview = AudioClipMinMaxOverview.GetOverviewFor(clip);

                if (preview == null)
                    throw new ArgumentException("Clip " + clip + "'s overview preview is null");

                looping = isLooping;

                this.localStart = localStart;
                this.localLength = localLength;

View on GitHub (pinned to 225b0fbdb5)

Solutions

  1. Guard the clip selection: skip preview rendering for clips with zero duration.
  2. Validate localLength > 0 before constructing ClipPreviewDetails.
  3. Ensure trim start/end values produce a positive difference.
  4. Use Mathf.Max(epsilon, localLength) or skip the preview if the computed length is below a small epsilon.

Example fix

// before
var details = new ClipPreviewDetails(clip, looping, size, start, localLength);
// after
if (localLength <= 0)
    return; // skip preview for zero-length segments
var details = new ClipPreviewDetails(clip, looping, size, start, localLength);
Defensive patterns

Strategy: validation

Validate before calling

// Validate localLength before constructing ClipPreviewDetails
if (localLength <= 0)
{
    Debug.LogWarning("Clip preview localLength must be positive. Skipping preview.");
    return;
}
var details = new ClipPreviewDetails(clip, looping, size, localStart, localLength);

Prevention

When it happens

Trigger: Constructing ClipPreviewDetails with localLength=0 or a negative value. Occurs when the clip's duration computes to zero (empty AudioClip), when a trim region has zero length, or when arithmetic on start/end times yields a non-positive difference.

Common situations: Empty or unimported AudioClips with zero samples, trim regions where end equals start, floating-point precision issues producing near-zero or slightly-negative lengths, corrupted or placeholder audio assets.

Related errors


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