{"record":{"id":"bdd74651d8dd7204","repo":"Unity-Technologies/UnityCsReference","slug":"index-must-be-between-0-and-instancecount-1-b-bdd746","errorCode":null,"errorMessage":"index must be between 0 and {instanceCount - 1}, but was {instanceIndex}","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":546,"sourceCode":"        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)\n        {\n            if (instanceIndex >= instanceCount)\n                throw new ArgumentException($\"index must be between 0 and {instanceCount - 1}, but was {instanceIndex}\");\n            Instance inst = instance(instanceIndex);\n            int submeshCount = inst.submeshMaterialIndices.Length;\n            if (submeshIndex >= submeshCount)\n                throw new ArgumentException($\"submeshIndex must be between 0 and {submeshCount - 1}, but was {submeshIndex}\");\n            int materialIndex = inst.submeshMaterialIndices[submeshIndex];\n            if (materialIndex == -1)","sourceCodeStart":528,"sourceCodeEnd":564,"githubUrl":"https://github.com/Unity-Technologies/UnityCsReference/blob/225b0fbdb57cc17d094e8056b71f8314aba56f73/Editor/Mono/GI/LightBaker.bindings.cs#L528-L564","documentation":"LightBaker.instanceToAlbedoIndex(uint instanceIndex) throws ArgumentException when instanceIndex >= instanceCount. It maps an instance to its albedo texture index, so the instance must be valid first.","triggerScenarios":"Calling instanceToAlbedoIndex with an index outside [0, instanceCount); using a global index instead of an instance index.","commonSituations":"Stale instanceCount after re-bake; off-by-one; confusing instance index with material/texture index.","solutions":["Verify instanceIndex < instanceCount before calling.","Re-read instanceCount after scene/bake changes.","Pair the returned albedo index with albedoTextureCount before using GetAlbedoTexture."],"exampleFix":"// before\nint aIdx = lightBaker.instanceToAlbedoIndex(instIdx);\n\n// after\nif (instIdx >= lightBaker.instanceCount) return -1;\nint aIdx = lightBaker.instanceToAlbedoIndex(instIdx);","handlingStrategy":"validation","validationCode":"if (instanceIndex < lightBaker.instanceCount)\n    int aIdx = lightBaker.instanceToAlbedoIndex(instanceIndex);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Verify instanceIndex < instanceCount before mapping.","After mapping, confirm aIdx < albedoTextureCount before GetAlbedoTexture.","Re-query instanceCount after bake/scene changes."],"tags":["gi","lightbaking","instance","albedo","validation","index","argument"],"backgroundTag":null,"analyzedSha":"225b0fbdb57cc17d094e8056b71f8314aba56f73","analyzedAt":"2026-08-13T19:07:19.849Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}