{"record":{"id":"c3b776d8ca718cad","repo":"Unity-Technologies/UnityCsReference","slug":"must-be-greater-than-zero","errorCode":null,"errorMessage":"Must be greater than zero.","messagePattern":"Must be greater than zero\\.","errorType":"validation","errorClass":"ArgumentOutOfRangeException","httpStatus":null,"severity":"error","filePath":"Editor/Mono/Audio/AudioRandomContainerExtensions.cs","lineNumber":25,"sourceCode":"using UnityEngine.Audio;\n\nnamespace UnityEditor;\n\nstatic class AudioRandomContainerExtensions\n{\n    const string k_BaseAddElementsUndoName = $\"Add {nameof(AudioRandomContainer)} element\";\n\n    /// <summary>\n    /// Adds a number of new, default-initialized <see cref=\"AudioContainerElement\"/> objects to <see cref=\"AudioRandomContainer.elements\"/>.\n    /// </summary>\n    /// <param name=\"container\">The instance to add the elements to.</param>\n    /// <param name=\"count\">The number of elements to add.</param>\n    internal static void AddElements(this AudioRandomContainer container, int count)\n    {\n        ValidateContainer(container);\n\n        if (count <= 0)\n            throw new ArgumentOutOfRangeException(nameof(count), \"Must be greater than zero.\");\n\n        AddElementsInner(container, count, (i, element) => { });\n    }\n\n    /// <summary>\n    /// Adds a given number of new <see cref=\"AudioContainerElement\"/> objects with clips assigned to <see cref=\"AudioRandomContainer.elements\"/>.\n    /// </summary>\n    /// <param name=\"container\">The instance to add the elements to.</param>\n    /// <param name=\"clips\">An array of <see cref=\"AudioClip\"/> objects to be assigned to each new element.</param>\n    internal static void AddElements(this AudioRandomContainer container, AudioClip[] clips)\n    {\n        ValidateContainer(container);\n\n        if (clips == null)\n            throw new ArgumentNullException(nameof(clips));\n\n        if (clips.Length == 0)\n            throw new ArgumentException(\"Must not be empty.\", nameof(clips));","sourceCodeStart":7,"sourceCodeEnd":43,"githubUrl":"https://github.com/Unity-Technologies/UnityCsReference/blob/225b0fbdb57cc17d094e8056b71f8314aba56f73/Editor/Mono/Audio/AudioRandomContainerExtensions.cs#L7-L43","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before\ncontainer.AddElements(requestedCount);\n// after\nif (requestedCount > 0)\n    container.AddElements(requestedCount);","handlingStrategy":"validation","validationCode":"// Validate count before calling AddElements\nvoid SafeAddElements(AudioRandomContainer container, int count)\n{\n    if (container == null) return;\n    if (count <= 0)\n    {\n        Debug.LogWarning(\"AddElements count must be greater than zero. Skipping.\");\n        return;\n    }\n    container.AddElements(count);\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["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."],"tags":["audio","editor","validation","argument-validation","audio-random-container"],"backgroundTag":null,"analyzedSha":"225b0fbdb57cc17d094e8056b71f8314aba56f73","analyzedAt":"2026-08-13T19:07:19.849Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}