{"record":{"id":"fc56e102ef20b866","repo":"Unity-Technologies/UnityCsReference","slug":"index-must-be-between-0-and-getlightcount-1","errorCode":null,"errorMessage":"index must be between 0 and {GetLightCount() - 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":531,"sourceCode":"                throw new ArgumentException($\"instanceIndex must be between 0 and {instanceCount - 1}, but was {instanceIndex}\");\n            Instance theInstance = instance(instanceIndex);\n            if (theInstance.submeshMaterialIndices.Length == 0)\n                throw new ArgumentException($\"instance {instanceIndex} has not materials\");\n            if (submeshIndex >= theInstance.submeshMaterialIndices.Length)\n                throw new ArgumentException($\"submeshIndex must be between 0 and {theInstance.submeshMaterialIndices.Length - 1}, but was {submeshIndex}\");\n            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)","sourceCodeStart":513,"sourceCodeEnd":549,"githubUrl":"https://github.com/Unity-Technologies/UnityCsReference/blob/225b0fbdb57cc17d094e8056b71f8314aba56f73/Editor/Mono/GI/LightBaker.bindings.cs#L513-L549","documentation":"LightBaker.GetLight(uint index) throws ArgumentException when index >= GetLightCount(). The light list is the set of lights contributing to the bake; GetLightCount() is a method (not a property) and can be 0 if no lights were baked.","triggerScenarios":"Calling GetLight with an index not bounded by GetLightCount(); calling before lights were added/baked.","commonSituations":"Assuming a non-zero light count; using a stale captured count; off-by-one.","solutions":["Call GetLightCount() and iterate [0, count).","Treat 0 as 'no baked lights' and skip.","Re-query GetLightCount() after bake changes."],"exampleFix":"// before\nvar l = lightBaker.GetLight(0);\n\n// after\nuint lc = lightBaker.GetLightCount();\nfor (uint i = 0; i < lc; i++)\n    Process(lightBaker.GetLight(i));","handlingStrategy":"validation","validationCode":"uint lc = lightBaker.GetLightCount();\nif (index < lc)\n    var light = lightBaker.GetLight(index);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["GetLightCount() is a method; call it fresh before indexing.","Handle 0 (no baked lights) gracefully.","Re-query after bake changes."],"tags":["gi","lightbaking","light","validation","index","argument"],"backgroundTag":null,"analyzedSha":"225b0fbdb57cc17d094e8056b71f8314aba56f73","analyzedAt":"2026-08-13T19:07:19.849Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}