{"record":{"id":"48870b0226a6dd23","repo":"Unity-Technologies/UnityCsReference","slug":"index-must-be-between-0-and-lightmapcount-1-b","errorCode":null,"errorMessage":"index must be between 0 and {lightmapCount - 1}, but was {index}","messagePattern":"index must be between 0 and (.+?), but was (.+?)","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"Editor/Mono/GI/LightBaker.bindings.cs","lineNumber":858,"sourceCode":"            if (_ownsPtr && _ptr != IntPtr.Zero)\n            {\n                Internal_Destroy(_ptr);\n                _ptr = IntPtr.Zero;\n            }\n        }\n\n        public extern ulong GetByteSize();\n        static extern IntPtr Internal_Create();\n        [NativeMethod(IsThreadSafe = true)]\n        static extern void Internal_Destroy(IntPtr ptr);\n\n        public extern LightmapRequest[] GetLightmapRequests();\n        public extern void SetLightmapRequests(LightmapRequest[] requests);\n\n        public uint lightmapInstanceCount(uint index)\n        {\n            if (index >= lightmapCount)\n                throw new ArgumentException($\"index must be between 0 and {lightmapCount - 1}, but was {index}\");\n            return Internal_InstanceCount(index);\n        }\n\n        public extern uint lightmapCount { get; }\n        extern uint Internal_LightmapWidth(uint index);\n        extern uint Internal_LightmapHeight(uint index);\n        extern uint Internal_InstanceCount(uint lightmapIndex);\n        public extern void SetLightmapResolution(Resolution resolution);\n        public extern void SetSingleLightmapResolution(uint index, Resolution resolution);\n        public Resolution lightmapResolution(uint index)\n        {\n            if (index >= lightmapCount)\n                throw new ArgumentException($\"index must be between 0 and {lightmapCount - 1}, but was {index}\");\n            Resolution resolution;\n            resolution.width = Internal_LightmapWidth(index);\n            resolution.height = Internal_LightmapHeight(index);\n            return resolution;\n        }","sourceCodeStart":840,"sourceCodeEnd":876,"githubUrl":"https://github.com/Unity-Technologies/UnityCsReference/blob/225b0fbdb57cc17d094e8056b71f8314aba56f73/Editor/Mono/GI/LightBaker.bindings.cs#L840-L876","documentation":"Thrown by LightmapRequests.lightmapInstanceCount when the uint lightmap index is >= lightmapCount. The lightmap index space is separate from instance, texture, and request indices; only lightmapCount is a valid bound. The method returns how many instances are baked into the given lightmap.","triggerScenarios":"Iterating lightmaps with the instance count, the LightmapRequest[] length, or a scene's lightmap index from renderer.lightmapIndex; calling after SetLightmapRequests changed lightmapCount.","commonSituations":"Readback code that assumes lightmapCount equals the number of renderers or scenes; stale LightmapRequests reused after the request set changed.","solutions":["Bound the loop with lightmapRequests.lightmapCount.","Re-query lightmapCount after any SetLightmapRequests call before iterating.","Guard: if (index < lmRequests.lightmapCount) { ... }"],"exampleFix":"// before\nfor (uint i = 0; i < requests.Length; i++)\n    n += lmRequests.lightmapInstanceCount(i);\n// after\nfor (uint i = 0; i < lmRequests.lightmapCount; i++)\n    n += lmRequests.lightmapInstanceCount(i);","handlingStrategy":"validation","validationCode":"if (index < lmRequests.lightmapCount)\n{\n    uint n = lmRequests.lightmapInstanceCount(index);\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Bound lightmap iteration with lightmapCount.","Re-query lightmapCount after SetLightmapRequests before iterating.","Do not pass renderer.lightmapIndex directly as the argument."],"tags":["lightmapping","gi","lightmap-requests","index-bounds","unity"],"backgroundTag":null,"analyzedSha":"225b0fbdb57cc17d094e8056b71f8314aba56f73","analyzedAt":"2026-08-13T19:07:19.849Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}