Unity-Technologies/UnityCsReference · error · ArgumentException

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

Error message

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

What it means

LightBaker.instance(uint index) getter throws ArgumentException when index >= instanceCount. An 'instance' is a baked geometry instance (e.g. a renderer/LOD contributing to the bake). The guard protects the native instance-array access.

Source

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

        static extern void Internal_Destroy(IntPtr ptr);

        extern LightingSettings Internal_GetLightingSettings();
        public LightingSettings GetLightingSettings()
        {
            return Internal_GetLightingSettings();
        }
        extern void Internal_SetLightingSettings(LightingSettings lightingSettings);
        public void SetLightingSettings(LightingSettings lightingSettings)
        {
            Internal_SetLightingSettings(lightingSettings);
        }

        public extern uint instanceCount { get; }
        extern Instance Internal_Instance(uint index);
        public Instance instance(uint index)
        {
            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);

View on GitHub (pinned to 225b0fbdb5)

Solutions

  1. Read instanceCount and iterate strictly [0, count).
  2. Re-query instanceCount after scene edits or re-bake before indexing.
  3. Use the returned Instance struct's own fields (e.g. submeshMaterialIndices) for further bounds, not assumptions.

Example fix

// before
Instance inst = lightBaker.instance(0);

// after
uint n = lightBaker.instanceCount;
for (uint i = 0; i < n; i++)
    Process(lightBaker.instance(i));
Defensive patterns

Strategy: validation

Validate before calling

uint n = lightBaker.instanceCount;
if (index < n)
    Instance inst = lightBaker.instance(index);

Prevention

When it happens

Trigger: Calling instance(i) for i outside [0, instanceCount); using a previously captured instanceCount that became stale after the scene/bake changed.

Common situations: Hardcoded instance indices after editing the scene; iterating with <= count; reading instanceCount before the bake populated instances.

Related errors


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