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

What it means

LightBaker.instanceToAlbedoIndex(uint instanceIndex) throws ArgumentException when instanceIndex >= instanceCount. It maps an instance to its albedo texture index, so the instance must be valid first.

Source

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

        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)
        {
            if (instanceIndex >= instanceCount)
                throw new ArgumentException($"index must be between 0 and {instanceCount - 1}, but was {instanceIndex}");
            Instance inst = instance(instanceIndex);
            int submeshCount = inst.submeshMaterialIndices.Length;
            if (submeshIndex >= submeshCount)
                throw new ArgumentException($"submeshIndex must be between 0 and {submeshCount - 1}, but was {submeshIndex}");
            int materialIndex = inst.submeshMaterialIndices[submeshIndex];
            if (materialIndex == -1)

View on GitHub (pinned to 225b0fbdb5)

Solutions

  1. Verify instanceIndex < instanceCount before calling.
  2. Re-read instanceCount after scene/bake changes.
  3. Pair the returned albedo index with albedoTextureCount before using GetAlbedoTexture.

Example fix

// before
int aIdx = lightBaker.instanceToAlbedoIndex(instIdx);

// after
if (instIdx >= lightBaker.instanceCount) return -1;
int aIdx = lightBaker.instanceToAlbedoIndex(instIdx);
Defensive patterns

Strategy: validation

Validate before calling

if (instanceIndex < lightBaker.instanceCount)
    int aIdx = lightBaker.instanceToAlbedoIndex(instanceIndex);

Prevention

When it happens

Trigger: Calling instanceToAlbedoIndex with an index outside [0, instanceCount); using a global index instead of an instance index.

Common situations: Stale instanceCount after re-bake; off-by-one; confusing instance index with material/texture index.

Related errors


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