Unity-Technologies/UnityCsReference · error · ArgumentException

index must be between 0 and {terrainCount - 1}, but was {ind

Error message

index must be between 0 and {terrainCount - 1}, but was {index}

What it means

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.

Source

Thrown at Editor/Mono/GI/LightBaker.bindings.cs:484

            if (index >= instanceCount)
                throw new ArgumentException($"index must be between 0 and {instanceCount - 1}, but was {index}");
            Instance instance = Internal_Instance(index);
            return instance;
        }
        extern void Internal_SetInstance(uint index, Instance instance);
        public void instance(uint index, Instance instance)
        {
            if (index >= instanceCount)
                throw new ArgumentException($"index must be between 0 and {instanceCount - 1}, but was {index}");
            Internal_SetInstance(index, instance);
        }

        public extern uint terrainCount { get; }
        extern Terrain Internal_GetTerrain(uint index);
        public Terrain GetTerrain(uint index)
        {
            if (index >= terrainCount)
                throw new ArgumentException($"index must be between 0 and {terrainCount - 1}, but was {index}");
            Terrain terrain = Internal_GetTerrain(index);
            return terrain;
        }

        public extern Vector2[] GetUV1VertexData(uint meshIndex);

        public extern uint meshCount { get; }
        public extern uint heightmapCount { get; }
        public extern uint holemapCount { get; }
        public extern uint materialCount { get; }
        extern Material Internal_GetMaterial(uint index);
        extern void Internal_SetMaterial(uint index, Material material);
        public Material GetMaterial(uint index)
        {
            if (index >= materialCount)
                throw new ArgumentException($"index must be between 0 and {materialCount - 1}, but was {index}");
            Material material = Internal_GetMaterial(index);
            return material;

View on GitHub (pinned to 225b0fbdb5)

Solutions

  1. Read terrainCount and iterate [0, count); skip when 0.
  2. Keep terrain indexing separate from instance/mesh indexing.
  3. Re-query terrainCount after scene/bake changes.

Example fix

// before
var t = lightBaker.GetTerrain(0);

// after
for (uint i = 0; i < lightBaker.terrainCount; i++)
    Process(lightBaker.GetTerrain(i));
Defensive patterns

Strategy: validation

Validate before calling

uint n = lightBaker.terrainCount;
if (index < n)
    var terrain = lightBaker.GetTerrain(index);

Prevention

When it happens

Trigger: Calling GetTerrain with an index not bounded by terrainCount; assuming a non-zero terrain count in a scene that has none.

Common situations: Generic 'iterate all baked objects' code that wrongly reuses mesh/instance bounds for terrains; calling after a re-bake removed terrains.

Related errors


AI-assisted analysis of Unity-Technologies/UnityCsReference@225b0fbdb5 (2026-08-13). Data as JSON: /api/errors/8b4ba3e7f2cc9cd4. Report an issue: GitHub.