{"record":{"id":"ac5f58a3c36d465c","repo":"Unity-Technologies/UnityCsReference","slug":"the-value-renderedpickingindexcount-must-not-b","errorCode":null,"errorMessage":"The value ({renderedPickingIndexCount}) must not be negative","messagePattern":"The value \\((.+?)\\) must not be negative","errorType":"validation","errorClass":"ArgumentOutOfRangeException","httpStatus":null,"severity":"error","filePath":"Editor/Mono/Handles/HandleUtility.cs","lineNumber":102,"sourceCode":"        {\n            var contained = RenderObjectSetContainsObject(obj);\n            return renderPickingType == RenderPickingType.RenderFromFilterSet ? contained : !contained;\n        }\n    }\n\n    public readonly struct RenderPickingResult\n    {\n        public int renderedPickingIndexCount { get; }\n        public HandleUtility.ResolvePickingCallback resolver { get; }\n        public HandleUtility.ResolvePickingWithWorldPositionCallback resolverWithWorldPos { get; }\n\n        [NoAutoStaticsCleanup] // default struct value; no mutable state\n        public static readonly RenderPickingResult NoOperation = default;\n\n        public RenderPickingResult(int renderedPickingIndexCount, HandleUtility.ResolvePickingCallback resolver)\n        {\n            if (renderedPickingIndexCount < 0)\n                throw new ArgumentOutOfRangeException(nameof(renderedPickingIndexCount), $\"The value ({renderedPickingIndexCount}) must not be negative\");\n            if (resolver == null)\n                throw new ArgumentNullException(nameof(resolver));\n\n            this.renderedPickingIndexCount = renderedPickingIndexCount;\n            this.resolver = resolver;\n            this.resolverWithWorldPos = null;\n        }\n\n        public RenderPickingResult(int renderedPickingIndexCount, HandleUtility.ResolvePickingWithWorldPositionCallback resolver)\n        {\n            if (renderedPickingIndexCount < 0)\n                throw new ArgumentOutOfRangeException(nameof(renderedPickingIndexCount), $\"The value ({renderedPickingIndexCount}) must not be negative\");\n            if (resolver == null)\n                throw new ArgumentNullException(nameof(resolver));\n\n            this.renderedPickingIndexCount = renderedPickingIndexCount;\n            this.resolverWithWorldPos = resolver;\n            this.resolver = null;","sourceCodeStart":84,"sourceCodeEnd":120,"githubUrl":"https://github.com/Unity-Technologies/UnityCsReference/blob/225b0fbdb57cc17d094e8056b71f8314aba56f73/Editor/Mono/Handles/HandleUtility.cs#L84-L120","documentation":"The RenderPickingResult constructor validates that renderedPickingIndexCount is non-negative; a negative count would break the picking-index arithmetic used to map a picked sub-mesh index back to a handle, so it throws ArgumentOutOfRangeException. Both constructor overloads apply the same guard, since the count is independent of which resolver callback is supplied.","triggerScenarios":"Constructing a RenderPickingResult with a negative count, e.g. subtracting control counts and underflowing; passing an uninitialized int field; computing the count from a mesh that failed to render.","commonSituations":"Custom editor handle implementing picking via RenderPickingResult; arithmetic that yields -1 when no handle control IDs are allocated; stale/zeroed buffers after a domain reload.","solutions":["Clamp the count to at least 0 (Math.Max(0, count)) before constructing the result.","Return RenderPickingResult.NoOperation (the default) when no picking indices are produced.","Audit the control-ID allocation arithmetic to ensure the count reflects actually rendered handles."],"exampleFix":"// before\nreturn new RenderPickingResult(nextControlId - firstControlId, Resolve);\n\n// after\nint count = Math.Max(0, nextControlId - firstControlId);\nreturn count == 0 ? RenderPickingResult.NoOperation : new RenderPickingResult(count, Resolve);","handlingStrategy":"validation","validationCode":"int count = Math.Max(0, nextControlId - firstControlId);\nif (count == 0) return RenderPickingResult.NoOperation;\nreturn new RenderPickingResult(count, Resolve);","typeGuard":"static bool IsValidPickingCount(int count) => count >= 0;","tryCatchPattern":null,"preventionTips":["Clamp picking counts to >= 0.","Return RenderPickingResult.NoOperation for zero-index cases.","Verify control-ID allocation arithmetic in custom handles."],"tags":["handles","picking","argument","editor-scripting","validation"],"backgroundTag":null,"analyzedSha":"225b0fbdb57cc17d094e8056b71f8314aba56f73","analyzedAt":"2026-08-13T19:07:19.849Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}