Unity-Technologies/UnityCsReference · error · ArgumentOutOfRangeException
Must be greater than zero.
Error message
Must be greater than zero.
What it means
Thrown by AudioRandomContainerExtensions.AddElements when the count parameter is zero or negative. This is a defensive guard ensuring at least one element is added per call, preventing no-op or nonsensical operations that would still register undo state.
Source
Thrown at Editor/Mono/Audio/AudioRandomContainerExtensions.cs:25
using UnityEngine.Audio;
namespace UnityEditor;
static class AudioRandomContainerExtensions
{
const string k_BaseAddElementsUndoName = $"Add {nameof(AudioRandomContainer)} element";
/// <summary>
/// Adds a number of new, default-initialized <see cref="AudioContainerElement"/> objects to <see cref="AudioRandomContainer.elements"/>.
/// </summary>
/// <param name="container">The instance to add the elements to.</param>
/// <param name="count">The number of elements to add.</param>
internal static void AddElements(this AudioRandomContainer container, int count)
{
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));View on GitHub (pinned to 225b0fbdb5)
Solutions
- Guard the call site: only invoke AddElements when count > 0.
- If count comes from user input in an editor window, validate and clamp the field before calling.
- If deriving count from an array length, check Length > 0 first or use the clips-array overload which already guards for emptiness.
- Review the calling code for off-by-one or sign errors in count computation.
Example fix
// before
container.AddElements(requestedCount);
// after
if (requestedCount > 0)
container.AddElements(requestedCount); Defensive patterns
Strategy: validation
Validate before calling
// Validate count before calling AddElements
void SafeAddElements(AudioRandomContainer container, int count)
{
if (container == null) return;
if (count <= 0)
{
Debug.LogWarning("AddElements count must be greater than zero. Skipping.");
return;
}
container.AddElements(count);
} Prevention
- Always validate count > 0 before calling AddElements(int).
- Clamp user-input count fields to a minimum of 1 in editor UIs.
- If count derives from an array, check Length > 0 first.
- Use the clips-array overload when adding clips, as it handles the count internally.
When it happens
Trigger: Calling container.AddElements(0) or container.AddElements(-1). Occurs when a UI button or script passes a user-entered or computed count without clamping it to a positive minimum, or when an array length calculation yields zero and is forwarded as a count.
Common situations: Editor tooling where a 'Add Elements' count field is left empty or set to 0, programmatic batch operations where a filtered clip list ends up empty, off-by-one errors in loop counts passed to AddElements.
Related errors
AI-assisted analysis of Unity-Technologies/UnityCsReference@225b0fbdb5 (2026-08-13).
Data as JSON: /api/errors/c3b776d8ca718cad.
Report an issue: GitHub.