{"record":{"id":"5717ca01adbf9632","repo":"Unity-Technologies/UnityCsReference","slug":"index-must-be-between-0-and-instancecount-1-b","errorCode":null,"errorMessage":"index must be between 0 and {instanceCount - 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":467,"sourceCode":"        static extern void Internal_Destroy(IntPtr ptr);\n\n        extern LightingSettings Internal_GetLightingSettings();\n        public LightingSettings GetLightingSettings()\n        {\n            return Internal_GetLightingSettings();\n        }\n        extern void Internal_SetLightingSettings(LightingSettings lightingSettings);\n        public void SetLightingSettings(LightingSettings lightingSettings)\n        {\n            Internal_SetLightingSettings(lightingSettings);\n        }\n\n        public extern uint instanceCount { get; }\n        extern Instance Internal_Instance(uint index);\n        public Instance instance(uint index)\n        {\n            if (index >= instanceCount)\n                throw new ArgumentException($\"index must be between 0 and {instanceCount - 1}, but was {index}\");\n            Instance instance = Internal_Instance(index);\n            return instance;\n        }\n        extern void Internal_SetInstance(uint index, Instance instance);\n        public void instance(uint index, Instance instance)\n        {\n            if (index >= instanceCount)\n                throw new ArgumentException($\"index must be between 0 and {instanceCount - 1}, but was {index}\");\n            Internal_SetInstance(index, instance);\n        }\n\n        public extern uint terrainCount { get; }\n        extern Terrain Internal_GetTerrain(uint index);\n        public Terrain GetTerrain(uint index)\n        {\n            if (index >= terrainCount)\n                throw new ArgumentException($\"index must be between 0 and {terrainCount - 1}, but was {index}\");\n            Terrain terrain = Internal_GetTerrain(index);","sourceCodeStart":449,"sourceCodeEnd":485,"githubUrl":"https://github.com/Unity-Technologies/UnityCsReference/blob/225b0fbdb57cc17d094e8056b71f8314aba56f73/Editor/Mono/GI/LightBaker.bindings.cs#L449-L485","documentation":"LightBaker.instance(uint index) getter throws ArgumentException when index >= instanceCount. An 'instance' is a baked geometry instance (e.g. a renderer/LOD contributing to the bake). The guard protects the native instance-array access.","triggerScenarios":"Calling instance(i) for i outside [0, instanceCount); using a previously captured instanceCount that became stale after the scene/bake changed.","commonSituations":"Hardcoded instance indices after editing the scene; iterating with <= count; reading instanceCount before the bake populated instances.","solutions":["Read instanceCount and iterate strictly [0, count).","Re-query instanceCount after scene edits or re-bake before indexing.","Use the returned Instance struct's own fields (e.g. submeshMaterialIndices) for further bounds, not assumptions."],"exampleFix":"// before\nInstance inst = lightBaker.instance(0);\n\n// after\nuint n = lightBaker.instanceCount;\nfor (uint i = 0; i < n; i++)\n    Process(lightBaker.instance(i));","handlingStrategy":"validation","validationCode":"uint n = lightBaker.instanceCount;\nif (index < n)\n    Instance inst = lightBaker.instance(index);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Read instanceCount once per pass and bound the loop to it.","Re-query after scene edits or re-bake.","Use the returned Instance's fields for any further sub-indexing."],"tags":["gi","lightbaking","validation","index","argument"],"backgroundTag":null,"analyzedSha":"225b0fbdb57cc17d094e8056b71f8314aba56f73","analyzedAt":"2026-08-13T19:07:19.849Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}