Unity-Technologies/UnityCsReference · error · ArgumentException

index must be between 0 and {albedoTextureCount - 1}, but wa

Error message

index must be between 0 and {albedoTextureCount - 1}, but was {index}

What it means

LightBaker.GetAlbedoTexture(uint index) throws ArgumentException when index is >= albedoTextureCount. The count is the number of albedo textures baked for the current light baking result; it can be 0 if baking has not produced output. The guard prevents out-of-range native access into the texture array.

Source

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

            Destroy();
            GC.SuppressFinalize(this);
        }

        void Destroy()
        {
            if (_ownsPtr && _ptr != IntPtr.Zero)
            {
                Internal_Destroy(_ptr);
                _ptr = IntPtr.Zero;
            }
        }

        public extern ulong GetByteSize();

        public Texture2D GetAlbedoTexture(uint index)
        {
            if (index >= albedoTextureCount)
                throw new ArgumentException($"index must be between 0 and {albedoTextureCount - 1}, but was {index}");
            TextureData textureData = GetAlbedoTextureData(index);
            Texture2D tex = new((int)textureData.resolution.width, (int)textureData.resolution.height, TextureFormat.RGBAFloat, false);
            tex.SetPixelData(textureData.data, 0);
            tex.filterMode = FilterMode.Point;
            tex.Apply();
            return tex;
        }

        public Texture2D GetEmissiveTexture(uint index)
        {
            if (index >= emissiveTextureCount)
                throw new ArgumentException($"index must be between 0 and {emissiveTextureCount - 1}, but was {index}");
            TextureData textureData = GetEmissiveTextureData(index);
            Texture2D tex = new((int)textureData.resolution.width, (int)textureData.resolution.height, TextureFormat.RGBAFloat, false);
            tex.SetPixelData(textureData.data, 0);
            tex.filterMode = FilterMode.Point;
            tex.Apply();
            return tex;

View on GitHub (pinned to 225b0fbdb5)

Solutions

  1. Read lightBaker.albedoTextureCount and only call GetAlbedoTexture for index in [0, count).
  2. Skip or warn when albedoTextureCount == 0 (no bake output available).
  3. Re-query the count after each bake completes before iterating textures.

Example fix

// before
var tex = lightBaker.GetAlbedoTexture(0);

// after
if (lightBaker.albedoTextureCount == 0)
    return;
for (uint i = 0; i < lightBaker.albedoTextureCount; i++)
    yield return lightBaker.GetAlbedoTexture(i);
Defensive patterns

Strategy: validation

Validate before calling

uint count = lightBaker.albedoTextureCount;
if (index < count)
    var tex = lightBaker.GetAlbedoTexture(index);

Prevention

When it happens

Trigger: Calling GetAlbedoTexture with a hardcoded or computed index without first reading albedoTextureCount; calling it before any bake has run (count is 0, so any index >= 0 trips it).

Common situations: Iterating with a stale count captured before a re-bake changed the array size; assuming at least one albedo texture exists after a failed/empty bake; off-by-one loop using <= instead of <.

Related errors


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