{"record":{"id":"ffcd780f60ed2a9e","repo":"Unity-Technologies/UnityCsReference","slug":"instanceindex-must-be-between-0-and-instancecount","errorCode":null,"errorMessage":"instanceIndex must be between 0 and {instanceCount - 1}, but was {instanceIndex}","messagePattern":"instanceIndex must be between 0 and (.+?), but was (.+?)","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"Editor/Mono/GI/LightBaker.bindings.cs","lineNumber":513,"sourceCode":"        extern Material Internal_GetMaterial(uint index);\n        extern void Internal_SetMaterial(uint index, Material material);\n        public Material GetMaterial(uint index)\n        {\n            if (index >= materialCount)\n                throw new ArgumentException($\"index must be between 0 and {materialCount - 1}, but was {index}\");\n            Material material = Internal_GetMaterial(index);\n            return material;\n        }\n        public void SetMaterial(uint index, Material material)\n        {\n            if (index >= materialCount)\n                throw new ArgumentException($\"index must be between 0 and {materialCount - 1}, but was {index}\");\n            Internal_SetMaterial(index, material);\n        }\n        public int GetMaterialIndex(uint instanceIndex, uint submeshIndex)\n        {\n            if (instanceIndex >= instanceCount)\n                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}\");","sourceCodeStart":495,"sourceCodeEnd":531,"githubUrl":"https://github.com/Unity-Technologies/UnityCsReference/blob/225b0fbdb57cc17d094e8056b71f8314aba56f73/Editor/Mono/GI/LightBaker.bindings.cs#L495-L531","documentation":"LightBaker.GetMaterialIndex(uint instanceIndex, uint submeshIndex) throws ArgumentException when instanceIndex >= instanceCount. This is the first of three sequential guards: instanceIndex range, then 'instance has materials', then submeshIndex range. The instance must be valid before its submeshes can be mapped to a material index.","triggerScenarios":"Calling GetMaterialIndex with an instanceIndex outside [0, instanceCount); passing a stale instance index after a re-bake changed the instance set.","commonSituations":"Reusing instance indices captured before a bake; off-by-one loops; confusing a global index with an instance index.","solutions":["Verify instanceIndex < instanceCount before calling.","Re-read instanceCount after scene/bake changes.","Then also guard submeshIndex against the instance's submeshMaterialIndices.Length (see following errors)."],"exampleFix":"// before\nint mIdx = lightBaker.GetMaterialIndex(instIdx, subIdx);\n\n// after\nif (instIdx >= lightBaker.instanceCount) return -1;\nint mIdx = lightBaker.GetMaterialIndex(instIdx, subIdx);","handlingStrategy":"validation","validationCode":"if (instanceIndex >= lightBaker.instanceCount) return;\nInstance inst = lightBaker.instance(instanceIndex);\nif (submeshIndex < inst.submeshMaterialIndices.Length)\n    int mIdx = lightBaker.GetMaterialIndex(instanceIndex, submeshIndex);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Satisfy all three GetMaterialIndex guards: instance range, non-empty materials, submesh range.","Read the Instance once and validate its submesh count before indexing.","Re-query instanceCount after scene/bake changes."],"tags":["gi","lightbaking","material","instance","validation","index","argument"],"backgroundTag":null,"analyzedSha":"225b0fbdb57cc17d094e8056b71f8314aba56f73","analyzedAt":"2026-08-13T19:07:19.849Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}