Unity-Technologies/UnityCsReference · error · ArgumentException

submeshIndex must be between 0 and {theInstance.submeshMater

Error message

submeshIndex must be between 0 and {theInstance.submeshMaterialIndices.Length - 1}, but was {submeshIndex}

What it means

GetMaterialIndex's third guard throws ArgumentException when submeshIndex >= theInstance.submeshMaterialIndices.Length. After the instance is valid and non-empty, the submesh index must fall within that instance's own submesh count, which varies per instance.

Source

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

                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}");
            return Internal_GetLight(index);
        }
        public void SetLight(uint index, Light light)
        {
            if (index >= GetLightCount())

View on GitHub (pinned to 225b0fbdb5)

Solutions

  1. Read the instance and bound submeshIndex to its submeshMaterialIndices.Length.
  2. Do not assume a uniform submesh count across instances.
  3. Use strict < for the loop bound.

Example fix

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

// after
Instance inst = lightBaker.instance(instIdx);
if (subIdx >= inst.submeshMaterialIndices.Length) continue;
int mIdx = lightBaker.GetMaterialIndex(instIdx, subIdx);
Defensive patterns

Strategy: validation

Validate before calling

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

Prevention

When it happens

Trigger: Calling GetMaterialIndex with a submeshIndex not bounded by the specific instance's submeshMaterialIndices.Length; reusing a submesh count from a different instance.

Common situations: Assuming all instances share the same submesh count; hardcoded submesh indices for multi-submesh meshes; iterating submeshes with <=.

Related errors


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