Unity-Technologies/UnityCsReference · error · ArgumentException

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

Error message

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

What it means

LightBaker.GetMaterialIndex(uint instanceIndex, uint submeshIndex) throws ArgumentException when instanceIndex >= instanceCount. This is the first of three sequential guards: instanceIndex range, then 'instance has materials', then submeshIndex range. The instance must be valid before its submeshes can be mapped to a material index.

Source

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

        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;
        }
        public void SetMaterial(uint index, Material material)
        {
            if (index >= materialCount)
                throw new ArgumentException($"index must be between 0 and {materialCount - 1}, but was {index}");
            Internal_SetMaterial(index, material);
        }
        public int GetMaterialIndex(uint instanceIndex, uint submeshIndex)
        {
            if (instanceIndex >= instanceCount)
                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}");

View on GitHub (pinned to 225b0fbdb5)

Solutions

  1. Verify instanceIndex < instanceCount before calling.
  2. Re-read instanceCount after scene/bake changes.
  3. Then also guard submeshIndex against the instance's submeshMaterialIndices.Length (see following errors).

Example fix

// before
int mIdx = lightBaker.GetMaterialIndex(instIdx, subIdx);

// after
if (instIdx >= lightBaker.instanceCount) return -1;
int mIdx = lightBaker.GetMaterialIndex(instIdx, subIdx);
Defensive patterns

Strategy: validation

Validate before calling

if (instanceIndex >= lightBaker.instanceCount) return;
Instance inst = lightBaker.instance(instanceIndex);
if (submeshIndex < inst.submeshMaterialIndices.Length)
    int mIdx = lightBaker.GetMaterialIndex(instanceIndex, submeshIndex);

Prevention

When it happens

Trigger: Calling GetMaterialIndex with an instanceIndex outside [0, instanceCount); passing a stale instance index after a re-bake changed the instance set.

Common situations: Reusing instance indices captured before a bake; off-by-one loops; confusing a global index with an instance index.

Related errors


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