{"record":{"id":"39a11c90988a1e4a","repo":"Unity-Technologies/UnityCsReference","slug":"index-must-be-between-0-and-getcookiecount-1","errorCode":null,"errorMessage":"index must be between 0 and {GetCookieCount() - 1}, but was {index}","messagePattern":"index must be between 0 and (.+?), but was (.+?)","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"Editor/Mono/GI/LightBaker.bindings.cs","lineNumber":670,"sourceCode":"            if (index >= transmissiveTexturePropertiesCount)\n                throw new ArgumentException($\"index must be between 0 and {transmissiveTexturePropertiesCount - 1}, but was {index}\");\n            return Internal_GetTransmissiveTextureProperties(index);\n        }\n        extern void Internal_SetTransmissiveTextureProperties(uint index, TextureProperties textureProperties);\n        public void SetTransmissiveTextureProperties(uint index, TextureProperties textureProperties)\n        {\n            if (index >= transmissiveTexturePropertiesCount)\n                throw new ArgumentException($\"index must be between 0 and {transmissiveTexturePropertiesCount - 1}, but was {index}\");\n            Internal_SetTransmissiveTextureProperties(index, textureProperties);\n        }\n\n        public extern uint GetCookieCount();\n        extern CookieData Internal_GetCookieData(uint index);\n        extern void Internal_SetCookieData(uint index, CookieData cookieData);\n        public CookieData GetCookieData(uint index)\n        {\n            if (index >= GetCookieCount())\n                throw new ArgumentException($\"index must be between 0 and {GetCookieCount() - 1}, but was {index}\");\n            return Internal_GetCookieData(index);\n        }\n        public void SetCookieData(uint index, CookieData cookieData)\n        {\n            if (index >= GetCookieCount())\n                throw new ArgumentException($\"index must be between 0 and {GetCookieCount() - 1}, but was {index}\");\n            Internal_SetCookieData(index, cookieData);\n        }\n        public extern void SetEnvironment(Vector4 color);\n        public extern void SetEnvironmentFromTextures(TextureData posX, TextureData negX, TextureData posY, TextureData negY, TextureData posZ, TextureData negZ);\n        public extern TextureData GetEnvironmentCubeTexture();\n\n        internal static class BindingsMarshaller\n        {\n            public static IntPtr ConvertToNative(BakeInput bakeInput) => bakeInput._ptr;\n        }\n        public extern bool CheckIntegrity();\n    }","sourceCodeStart":652,"sourceCodeEnd":688,"githubUrl":"https://github.com/Unity-Technologies/UnityCsReference/blob/225b0fbdb57cc17d094e8056b71f8314aba56f73/Editor/Mono/GI/LightBaker.bindings.cs#L652-L688","documentation":"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.","triggerScenarios":"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.","commonSituations":"Scenes with no cookies (count 0, any read throws); caching GetCookieCount() once and then mutating the input so the count changes.","solutions":["Re-read GetCookieCount() each iteration or cache it only across a read-only window.","Skip cookie readback when GetCookieCount() == 0.","Guard: if (index < bakeInput.GetCookieCount()) { ... }"],"exampleFix":"// before\nfor (uint i = 0; i < lightCount; i++)\n    cookies.Add(bakeInput.GetCookieData(i));\n// after\nfor (uint i = 0; i < bakeInput.GetCookieCount(); i++)\n    cookies.Add(bakeInput.GetCookieData(i));","handlingStrategy":"validation","validationCode":"if (index < bakeInput.GetCookieCount())\n{\n    var c = bakeInput.GetCookieData(index);\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Use GetCookieCount() (the method) as the bound, not a light/instance count.","Skip readback when GetCookieCount() == 0.","Do not cache the count across mutations to the BakeInput."],"tags":["lightmapping","gi","bake-input","index-bounds","cookie","unity"],"backgroundTag":null,"analyzedSha":"225b0fbdb57cc17d094e8056b71f8314aba56f73","analyzedAt":"2026-08-13T19:07:19.849Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}