Unity-Technologies/UnityCsReference · error · ArgumentException
index must be between 0 and {lightmapCount - 1}, but was {in
Error message
index must be between 0 and {lightmapCount - 1}, but was {index} What it means
Thrown by LightmapRequests.lightmapInstanceCount when the uint lightmap index is >= lightmapCount. The lightmap index space is separate from instance, texture, and request indices; only lightmapCount is a valid bound. The method returns how many instances are baked into the given lightmap.
Source
Thrown at Editor/Mono/GI/LightBaker.bindings.cs:858
if (_ownsPtr && _ptr != IntPtr.Zero)
{
Internal_Destroy(_ptr);
_ptr = IntPtr.Zero;
}
}
public extern ulong GetByteSize();
static extern IntPtr Internal_Create();
[NativeMethod(IsThreadSafe = true)]
static extern void Internal_Destroy(IntPtr ptr);
public extern LightmapRequest[] GetLightmapRequests();
public extern void SetLightmapRequests(LightmapRequest[] requests);
public uint lightmapInstanceCount(uint index)
{
if (index >= lightmapCount)
throw new ArgumentException($"index must be between 0 and {lightmapCount - 1}, but was {index}");
return Internal_InstanceCount(index);
}
public extern uint lightmapCount { get; }
extern uint Internal_LightmapWidth(uint index);
extern uint Internal_LightmapHeight(uint index);
extern uint Internal_InstanceCount(uint lightmapIndex);
public extern void SetLightmapResolution(Resolution resolution);
public extern void SetSingleLightmapResolution(uint index, Resolution resolution);
public Resolution lightmapResolution(uint index)
{
if (index >= lightmapCount)
throw new ArgumentException($"index must be between 0 and {lightmapCount - 1}, but was {index}");
Resolution resolution;
resolution.width = Internal_LightmapWidth(index);
resolution.height = Internal_LightmapHeight(index);
return resolution;
}View on GitHub (pinned to 225b0fbdb5)
Solutions
- Bound the loop with lightmapRequests.lightmapCount.
- Re-query lightmapCount after any SetLightmapRequests call before iterating.
- Guard: if (index < lmRequests.lightmapCount) { ... }
Example fix
// before
for (uint i = 0; i < requests.Length; i++)
n += lmRequests.lightmapInstanceCount(i);
// after
for (uint i = 0; i < lmRequests.lightmapCount; i++)
n += lmRequests.lightmapInstanceCount(i); Defensive patterns
Strategy: validation
Validate before calling
if (index < lmRequests.lightmapCount)
{
uint n = lmRequests.lightmapInstanceCount(index);
} Prevention
- Bound lightmap iteration with lightmapCount.
- Re-query lightmapCount after SetLightmapRequests before iterating.
- Do not pass renderer.lightmapIndex directly as the argument.
When it happens
Trigger: Iterating lightmaps with the instance count, the LightmapRequest[] length, or a scene's lightmap index from renderer.lightmapIndex; calling after SetLightmapRequests changed lightmapCount.
Common situations: Readback code that assumes lightmapCount equals the number of renderers or scenes; stale LightmapRequests reused after the request set changed.
Related errors
- index must be between 0 and {albedoTexturePropertiesCount -
- index must be between 0 and {emissiveTexturePropertiesCount
- index must be between 0 and {transmissiveTextureCount - 1},
- index must be between 0 and {transmissiveTexturePropertiesCo
- index must be between 0 and {GetCookieCount() - 1}, but was
AI-assisted analysis of Unity-Technologies/UnityCsReference@225b0fbdb5 (2026-08-13).
Data as JSON: /api/errors/48870b0226a6dd23.
Report an issue: GitHub.