Unity-Technologies/UnityCsReference · error · ArgumentException

index must be between 0 and {GetCookieCount() - 1}, but was

Error message

index must be between 0 and {GetCookieCount() - 1}, but was {index}

What it means

Thrown by BakeInput.GetCookieData when the uint index is >= GetCookieCount(). Note the bound here is a method call (GetCookieCount()), not a property; it reflects the number of light cookie slots currently held by the bake input.

Source

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

            if (index >= transmissiveTexturePropertiesCount)
                throw new ArgumentException($"index must be between 0 and {transmissiveTexturePropertiesCount - 1}, but was {index}");
            return Internal_GetTransmissiveTextureProperties(index);
        }
        extern void Internal_SetTransmissiveTextureProperties(uint index, TextureProperties textureProperties);
        public void SetTransmissiveTextureProperties(uint index, TextureProperties textureProperties)
        {
            if (index >= transmissiveTexturePropertiesCount)
                throw new ArgumentException($"index must be between 0 and {transmissiveTexturePropertiesCount - 1}, but was {index}");
            Internal_SetTransmissiveTextureProperties(index, textureProperties);
        }

        public extern uint GetCookieCount();
        extern CookieData Internal_GetCookieData(uint index);
        extern void Internal_SetCookieData(uint index, CookieData cookieData);
        public CookieData GetCookieData(uint index)
        {
            if (index >= GetCookieCount())
                throw new ArgumentException($"index must be between 0 and {GetCookieCount() - 1}, but was {index}");
            return Internal_GetCookieData(index);
        }
        public void SetCookieData(uint index, CookieData cookieData)
        {
            if (index >= GetCookieCount())
                throw new ArgumentException($"index must be between 0 and {GetCookieCount() - 1}, but was {index}");
            Internal_SetCookieData(index, cookieData);
        }
        public extern void SetEnvironment(Vector4 color);
        public extern void SetEnvironmentFromTextures(TextureData posX, TextureData negX, TextureData posY, TextureData negY, TextureData posZ, TextureData negZ);
        public extern TextureData GetEnvironmentCubeTexture();

        internal static class BindingsMarshaller
        {
            public static IntPtr ConvertToNative(BakeInput bakeInput) => bakeInput._ptr;
        }
        public extern bool CheckIntegrity();
    }

View on GitHub (pinned to 225b0fbdb5)

Solutions

  1. Re-read GetCookieCount() each iteration or cache it only across a read-only window.
  2. Skip cookie readback when GetCookieCount() == 0.
  3. Guard: if (index < bakeInput.GetCookieCount()) { ... }

Example fix

// before
for (uint i = 0; i < lightCount; i++)
    cookies.Add(bakeInput.GetCookieData(i));
// after
for (uint i = 0; i < bakeInput.GetCookieCount(); i++)
    cookies.Add(bakeInput.GetCookieData(i));
Defensive patterns

Strategy: validation

Validate before calling

if (index < bakeInput.GetCookieCount())
{
    var c = bakeInput.GetCookieData(index);
}

Prevention

When it happens

Trigger: Iterating cookie data with a stale or different count (e.g., light count, instance count); reading cookie data before cookies were extracted into the BakeInput.

Common situations: Scenes with no cookies (count 0, any read throws); caching GetCookieCount() once and then mutating the input so the count changes.

Related errors


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