{"record":{"id":"55205a98c9ccf557","repo":"Unity-Technologies/UnityCsReference","slug":"submeshindex-must-be-between-0-and-theinstance-su","errorCode":null,"errorMessage":"submeshIndex must be between 0 and {theInstance.submeshMaterialIndices.Length - 1}, but was {submeshIndex}","messagePattern":"submeshIndex must be between 0 and (.+?), but was (.+?)","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"Editor/Mono/GI/LightBaker.bindings.cs","lineNumber":518,"sourceCode":"                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}\");\n            return Internal_GetLight(index);\n        }\n        public void SetLight(uint index, Light light)\n        {\n            if (index >= GetLightCount())","sourceCodeStart":500,"sourceCodeEnd":536,"githubUrl":"https://github.com/Unity-Technologies/UnityCsReference/blob/225b0fbdb57cc17d094e8056b71f8314aba56f73/Editor/Mono/GI/LightBaker.bindings.cs#L500-L536","documentation":"GetMaterialIndex's third guard throws ArgumentException when submeshIndex >= theInstance.submeshMaterialIndices.Length. After the instance is valid and non-empty, the submesh index must fall within that instance's own submesh count, which varies per instance.","triggerScenarios":"Calling GetMaterialIndex with a submeshIndex not bounded by the specific instance's submeshMaterialIndices.Length; reusing a submesh count from a different instance.","commonSituations":"Assuming all instances share the same submesh count; hardcoded submesh indices for multi-submesh meshes; iterating submeshes with <=.","solutions":["Read the instance and bound submeshIndex to its submeshMaterialIndices.Length.","Do not assume a uniform submesh count across instances.","Use strict < for the loop bound."],"exampleFix":"// before\nint mIdx = lightBaker.GetMaterialIndex(instIdx, subIdx);\n\n// after\nInstance inst = lightBaker.instance(instIdx);\nif (subIdx >= inst.submeshMaterialIndices.Length) continue;\nint mIdx = lightBaker.GetMaterialIndex(instIdx, subIdx);","handlingStrategy":"validation","validationCode":"Instance inst = lightBaker.instance(instanceIndex);\nif (submeshIndex < inst.submeshMaterialIndices.Length)\n    int mIdx = lightBaker.GetMaterialIndex(instanceIndex, submeshIndex);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Bound submeshIndex per-instance; submesh counts vary.","Never assume a uniform submesh count across instances.","Use strict < comparisons."],"tags":["gi","lightbaking","material","submesh","validation","index","argument"],"backgroundTag":null,"analyzedSha":"225b0fbdb57cc17d094e8056b71f8314aba56f73","analyzedAt":"2026-08-13T19:07:19.849Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}