Unity-Technologies/UnityCsReference · error · ArgumentException

index must be between 0 and {GetLightCount() - 1}, but was {

Error message

index must be between 0 and {GetLightCount() - 1}, but was {index}

What it means

LightBaker.GetLight(uint index) throws ArgumentException when index >= GetLightCount(). The light list is the set of lights contributing to the bake; GetLightCount() is a method (not a property) and can be 0 if no lights were baked.

Source

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

                throw new ArgumentException($"instanceIndex must be between 0 and {instanceCount - 1}, but was {instanceIndex}");
            Instance theInstance = instance(instanceIndex);
            if (theInstance.submeshMaterialIndices.Length == 0)
                throw new ArgumentException($"instance {instanceIndex} has not materials");
            if (submeshIndex >= theInstance.submeshMaterialIndices.Length)
                throw new ArgumentException($"submeshIndex must be between 0 and {theInstance.submeshMaterialIndices.Length - 1}, but was {submeshIndex}");
            return theInstance.submeshMaterialIndices[submeshIndex];
        }
        extern uint Internal_GetLightCount();
        public uint GetLightCount()
        {
            return Internal_GetLightCount();
        }
        extern Light Internal_GetLight(uint index);
        extern void Internal_SetLight(uint index, Light light);
        public Light GetLight(uint index)
        {
            if (index >= GetLightCount())
                throw new ArgumentException($"index must be between 0 and {GetLightCount() - 1}, but was {index}");
            return Internal_GetLight(index);
        }
        public void SetLight(uint index, Light light)
        {
            if (index >= GetLightCount())
                throw new ArgumentException($"index must be between 0 and {GetLightCount() - 1}, but was {0}");
            Internal_SetLight(index, light);
        }

        extern int Internal_instanceAlbedoEmissiveIndex(uint instanceIndex);
        extern int Internal_instanceTransmissiveIndex(uint instanceIndex, uint submeshIndex);
        public int instanceToAlbedoIndex(uint instanceIndex)
        {
            if (instanceIndex >= instanceCount)
                throw new ArgumentException($"index must be between 0 and {instanceCount - 1}, but was {instanceIndex}");
            return Internal_instanceAlbedoEmissiveIndex(instanceIndex);
        }
        public int instanceToEmissiveIndex(uint instanceIndex)

View on GitHub (pinned to 225b0fbdb5)

Solutions

  1. Call GetLightCount() and iterate [0, count).
  2. Treat 0 as 'no baked lights' and skip.
  3. Re-query GetLightCount() after bake changes.

Example fix

// before
var l = lightBaker.GetLight(0);

// after
uint lc = lightBaker.GetLightCount();
for (uint i = 0; i < lc; i++)
    Process(lightBaker.GetLight(i));
Defensive patterns

Strategy: validation

Validate before calling

uint lc = lightBaker.GetLightCount();
if (index < lc)
    var light = lightBaker.GetLight(index);

Prevention

When it happens

Trigger: Calling GetLight with an index not bounded by GetLightCount(); calling before lights were added/baked.

Common situations: Assuming a non-zero light count; using a stale captured count; off-by-one.

Related errors


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