{"record":{"id":"8b4ba3e7f2cc9cd4","repo":"Unity-Technologies/UnityCsReference","slug":"index-must-be-between-0-and-terraincount-1-bu","errorCode":null,"errorMessage":"index must be between 0 and {terrainCount - 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":484,"sourceCode":"            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);\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;","sourceCodeStart":466,"sourceCodeEnd":502,"githubUrl":"https://github.com/Unity-Technologies/UnityCsReference/blob/225b0fbdb57cc17d094e8056b71f8314aba56f73/Editor/Mono/GI/LightBaker.bindings.cs#L466-L502","documentation":"LightBaker.GetTerrain(uint index) throws ArgumentException when index >= terrainCount. Terrains are a separate baked-object category with their own count, which can be 0 in scenes without Terrain objects.","triggerScenarios":"Calling GetTerrain with an index not bounded by terrainCount; assuming a non-zero terrain count in a scene that has none.","commonSituations":"Generic 'iterate all baked objects' code that wrongly reuses mesh/instance bounds for terrains; calling after a re-bake removed terrains.","solutions":["Read terrainCount and iterate [0, count); skip when 0.","Keep terrain indexing separate from instance/mesh indexing.","Re-query terrainCount after scene/bake changes."],"exampleFix":"// before\nvar t = lightBaker.GetTerrain(0);\n\n// after\nfor (uint i = 0; i < lightBaker.terrainCount; i++)\n    Process(lightBaker.GetTerrain(i));","handlingStrategy":"validation","validationCode":"uint n = lightBaker.terrainCount;\nif (index < n)\n    var terrain = lightBaker.GetTerrain(index);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Index terrains with terrainCount, never with instance/mesh counts.","Handle terrainCount == 0 for scenes without Terrain objects.","Re-query after bake/scene changes."],"tags":["gi","lightbaking","terrain","validation","index","argument"],"backgroundTag":null,"analyzedSha":"225b0fbdb57cc17d094e8056b71f8314aba56f73","analyzedAt":"2026-08-13T19:07:19.849Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}