Unity-Technologies/UnityCsReference · error · ArgumentException
index must be between 0 and {materialCount - 1}, but was {in
Error message
index must be between 0 and {materialCount - 1}, but was {index} What it means
LightBaker.GetMaterial(uint index) throws ArgumentException when index >= materialCount. The material list holds materials referenced by baked instances/submeshes; count reflects how many distinct materials were baked.
Source
Thrown at Editor/Mono/GI/LightBaker.bindings.cs:500
{
if (index >= terrainCount)
throw new ArgumentException($"index must be between 0 and {terrainCount - 1}, but was {index}");
Terrain terrain = Internal_GetTerrain(index);
return terrain;
}
public extern Vector2[] GetUV1VertexData(uint meshIndex);
public extern uint meshCount { get; }
public extern uint heightmapCount { get; }
public extern uint holemapCount { get; }
public extern uint materialCount { get; }
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}");View on GitHub (pinned to 225b0fbdb5)
Solutions
- Read materialCount and iterate [0, count).
- Use GetMaterialIndex(instanceIndex, submeshIndex) to map a submesh to a global material index before calling GetMaterial.
- Re-query materialCount after material/scene changes.
Example fix
// before
var mat = lightBaker.GetMaterial(globalIndex);
// after
if (globalIndex < lightBaker.materialCount)
var mat = lightBaker.GetMaterial(globalIndex); Defensive patterns
Strategy: validation
Validate before calling
uint n = lightBaker.materialCount;
if (index < n)
var mat = lightBaker.GetMaterial(index); Prevention
- Map submesh -> global material index via GetMaterialIndex before GetMaterial.
- Do not confuse per-instance submesh indices with global material indices.
- Re-query materialCount after material/scene changes.
When it happens
Trigger: Calling GetMaterial with an index not bounded by materialCount; using a submesh/material index from one instance as a global material index.
Common situations: Confusing per-instance submesh indices with global material indices; stale materialCount after a re-bake that changed materials.
Related errors
- instanceIndex must be between 0 and {instanceCount - 1}, but
- submeshIndex must be between 0 and {theInstance.submeshMater
- index must be between 0 and {albedoTextureCount - 1}, but wa
- index must be between 0 and {emissiveTextureCount - 1}, but
- index must be between 0 and {instanceCount - 1}, but was {in
AI-assisted analysis of Unity-Technologies/UnityCsReference@225b0fbdb5 (2026-08-13).
Data as JSON: /api/errors/6b55c7725c213c64.
Report an issue: GitHub.