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 {0}

What it means

SetLight(uint index, Light light) throws ArgumentException when index >= GetLightCount(). NOTE a bug in the source: the message interpolates {0} instead of {index}, so it always reports 'was 0' regardless of the actual index. The guard itself is correct; only the diagnostic text is misleading. Treat any SetLight ArgumentException on a non-zero index as a possible out-of-range with a wrong reported value.

Source

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

            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)
        {
            if (instanceIndex >= instanceCount)
                throw new ArgumentException($"index must be between 0 and {instanceCount - 1}, but was {instanceIndex}");
            return Internal_instanceAlbedoEmissiveIndex(instanceIndex);
        }
        public int instanceToTransmissiveIndex(uint instanceIndex, uint submeshIndex)

View on GitHub (pinned to 225b0fbdb5)

Solutions

  1. Verify index < GetLightCount() before SetLight.
  2. When debugging this exception, ignore the reported 'was 0' and inspect the actual argument — the message is known-buggy.
  3. Report/follow the upstream fix so the message uses {index} (or nameof).

Example fix

// before
lightBaker.SetLight(i, light);

// after
if (i < lightBaker.GetLightCount())
    lightBaker.SetLight(i, light);
Defensive patterns

Strategy: validation

Validate before calling

uint lc = lightBaker.GetLightCount();
if (index < lc)
    lightBaker.SetLight(index, light);

Try / catch

// The exception message reports 'was 0' due to a {0} bug; do not trust it.
try { lightBaker.SetLight(i, light); }
catch (ArgumentException ex) when (ex.Message.Contains("GetLightCount"))
{
    // i was actually >= GetLightCount(); re-query and handle.
}

Prevention

When it happens

Trigger: Calling SetLight with an index not bounded by GetLightCount(); the exception message will misleadingly claim the index was 0 even when it was not.

Common situations: Stale light count after bake changes; off-by-one; debugging confusion because the message says 'was 0' while the call used a different index.

Related errors


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