{"record":{"id":"6b55c7725c213c64","repo":"Unity-Technologies/UnityCsReference","slug":"index-must-be-between-0-and-materialcount-1-b","errorCode":null,"errorMessage":"index must be between 0 and {materialCount - 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":500,"sourceCode":"        {\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);\n            return terrain;\n        }\n\n        public extern Vector2[] GetUV1VertexData(uint meshIndex);\n\n        public extern uint meshCount { get; }\n        public extern uint heightmapCount { get; }\n        public extern uint holemapCount { get; }\n        public extern uint materialCount { get; }\n        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}\");","sourceCodeStart":482,"sourceCodeEnd":518,"githubUrl":"https://github.com/Unity-Technologies/UnityCsReference/blob/225b0fbdb57cc17d094e8056b71f8314aba56f73/Editor/Mono/GI/LightBaker.bindings.cs#L482-L518","documentation":"LightBaker.GetMaterial(uint index) throws ArgumentException when index >= materialCount. The material list holds materials referenced by baked instances/submeshes; count reflects how many distinct materials were baked.","triggerScenarios":"Calling GetMaterial with an index not bounded by materialCount; using a submesh/material index from one instance as a global material index.","commonSituations":"Confusing per-instance submesh indices with global material indices; stale materialCount after a re-bake that changed materials.","solutions":["Read materialCount and iterate [0, count).","Use GetMaterialIndex(instanceIndex, submeshIndex) to map a submesh to a global material index before calling GetMaterial.","Re-query materialCount after material/scene changes."],"exampleFix":"// before\nvar mat = lightBaker.GetMaterial(globalIndex);\n\n// after\nif (globalIndex < lightBaker.materialCount)\n    var mat = lightBaker.GetMaterial(globalIndex);","handlingStrategy":"validation","validationCode":"uint n = lightBaker.materialCount;\nif (index < n)\n    var mat = lightBaker.GetMaterial(index);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Map submesh -> global material index via GetMaterialIndex before GetMaterial.","Do not confuse per-instance submesh indices with global material indices.","Re-query materialCount after material/scene changes."],"tags":["gi","lightbaking","material","validation","index","argument"],"backgroundTag":null,"analyzedSha":"225b0fbdb57cc17d094e8056b71f8314aba56f73","analyzedAt":"2026-08-13T19:07:19.849Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}