{"record":{"id":"ed6e131f72068a45","repo":"Unity-Technologies/UnityCsReference","slug":"index-must-be-between-0-and-getlightcount-1-ed6e13","errorCode":null,"errorMessage":"index must be between 0 and {GetLightCount() - 1}, but was {0}","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":537,"sourceCode":"            return theInstance.submeshMaterialIndices[submeshIndex];\n        }\n        extern uint Internal_GetLightCount();\n        public uint GetLightCount()\n        {\n            return Internal_GetLightCount();\n        }\n        extern Light Internal_GetLight(uint index);\n        extern void Internal_SetLight(uint index, Light light);\n        public Light GetLight(uint index)\n        {\n            if (index >= GetLightCount())\n                throw new ArgumentException($\"index must be between 0 and {GetLightCount() - 1}, but was {index}\");\n            return Internal_GetLight(index);\n        }\n        public void SetLight(uint index, Light light)\n        {\n            if (index >= GetLightCount())\n                throw new ArgumentException($\"index must be between 0 and {GetLightCount() - 1}, but was {0}\");\n            Internal_SetLight(index, light);\n        }\n\n        extern int Internal_instanceAlbedoEmissiveIndex(uint instanceIndex);\n        extern int Internal_instanceTransmissiveIndex(uint instanceIndex, uint submeshIndex);\n        public int instanceToAlbedoIndex(uint instanceIndex)\n        {\n            if (instanceIndex >= instanceCount)\n                throw new ArgumentException($\"index must be between 0 and {instanceCount - 1}, but was {instanceIndex}\");\n            return Internal_instanceAlbedoEmissiveIndex(instanceIndex);\n        }\n        public int instanceToEmissiveIndex(uint instanceIndex)\n        {\n            if (instanceIndex >= instanceCount)\n                throw new ArgumentException($\"index must be between 0 and {instanceCount - 1}, but was {instanceIndex}\");\n            return Internal_instanceAlbedoEmissiveIndex(instanceIndex);\n        }\n        public int instanceToTransmissiveIndex(uint instanceIndex, uint submeshIndex)","sourceCodeStart":519,"sourceCodeEnd":555,"githubUrl":"https://github.com/Unity-Technologies/UnityCsReference/blob/225b0fbdb57cc17d094e8056b71f8314aba56f73/Editor/Mono/GI/LightBaker.bindings.cs#L519-L555","documentation":"SetLight(uint index, Light light) throws ArgumentException when index >= GetLightCount(). NOTE a bug in the source: the message interpolates {0} instead of {index}, so it always reports 'was 0' regardless of the actual index. The guard itself is correct; only the diagnostic text is misleading. Treat any SetLight ArgumentException on a non-zero index as a possible out-of-range with a wrong reported value.","triggerScenarios":"Calling SetLight with an index not bounded by GetLightCount(); the exception message will misleadingly claim the index was 0 even when it was not.","commonSituations":"Stale light count after bake changes; off-by-one; debugging confusion because the message says 'was 0' while the call used a different index.","solutions":["Verify index < GetLightCount() before SetLight.","When debugging this exception, ignore the reported 'was 0' and inspect the actual argument — the message is known-buggy.","Report/follow the upstream fix so the message uses {index} (or nameof)."],"exampleFix":"// before\nlightBaker.SetLight(i, light);\n\n// after\nif (i < lightBaker.GetLightCount())\n    lightBaker.SetLight(i, light);","handlingStrategy":"validation","validationCode":"uint lc = lightBaker.GetLightCount();\nif (index < lc)\n    lightBaker.SetLight(index, light);","typeGuard":null,"tryCatchPattern":"// The exception message reports 'was 0' due to a {0} bug; do not trust it.\ntry { lightBaker.SetLight(i, light); }\ncatch (ArgumentException ex) when (ex.Message.Contains(\"GetLightCount\"))\n{\n    // i was actually >= GetLightCount(); re-query and handle.\n}","preventionTips":["Verify index < GetLightCount() before SetLight.","When debugging, ignore the misleading 'was 0' in the message and inspect the real index.","Track the upstream fix so the message interpolates {index}."],"tags":["gi","lightbaking","light","validation","index","argument","known-bug"],"backgroundTag":null,"analyzedSha":"225b0fbdb57cc17d094e8056b71f8314aba56f73","analyzedAt":"2026-08-13T19:07:19.849Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}