{"record":{"id":"f835ea381c012b0a","repo":"Unity-Technologies/UnityCsReference","slug":"index-must-be-between-0-and-albedotexturecount","errorCode":null,"errorMessage":"index must be between 0 and {albedoTextureCount - 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":426,"sourceCode":"            Destroy();\n            GC.SuppressFinalize(this);\n        }\n\n        void Destroy()\n        {\n            if (_ownsPtr && _ptr != IntPtr.Zero)\n            {\n                Internal_Destroy(_ptr);\n                _ptr = IntPtr.Zero;\n            }\n        }\n\n        public extern ulong GetByteSize();\n\n        public Texture2D GetAlbedoTexture(uint index)\n        {\n            if (index >= albedoTextureCount)\n                throw new ArgumentException($\"index must be between 0 and {albedoTextureCount - 1}, but was {index}\");\n            TextureData textureData = GetAlbedoTextureData(index);\n            Texture2D tex = new((int)textureData.resolution.width, (int)textureData.resolution.height, TextureFormat.RGBAFloat, false);\n            tex.SetPixelData(textureData.data, 0);\n            tex.filterMode = FilterMode.Point;\n            tex.Apply();\n            return tex;\n        }\n\n        public Texture2D GetEmissiveTexture(uint index)\n        {\n            if (index >= emissiveTextureCount)\n                throw new ArgumentException($\"index must be between 0 and {emissiveTextureCount - 1}, but was {index}\");\n            TextureData textureData = GetEmissiveTextureData(index);\n            Texture2D tex = new((int)textureData.resolution.width, (int)textureData.resolution.height, TextureFormat.RGBAFloat, false);\n            tex.SetPixelData(textureData.data, 0);\n            tex.filterMode = FilterMode.Point;\n            tex.Apply();\n            return tex;","sourceCodeStart":408,"sourceCodeEnd":444,"githubUrl":"https://github.com/Unity-Technologies/UnityCsReference/blob/225b0fbdb57cc17d094e8056b71f8314aba56f73/Editor/Mono/GI/LightBaker.bindings.cs#L408-L444","documentation":"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.","triggerScenarios":"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).","commonSituations":"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 <.","solutions":["Read lightBaker.albedoTextureCount and only call GetAlbedoTexture for index in [0, count).","Skip or warn when albedoTextureCount == 0 (no bake output available).","Re-query the count after each bake completes before iterating textures."],"exampleFix":"// before\nvar tex = lightBaker.GetAlbedoTexture(0);\n\n// after\nif (lightBaker.albedoTextureCount == 0)\n    return;\nfor (uint i = 0; i < lightBaker.albedoTextureCount; i++)\n    yield return lightBaker.GetAlbedoTexture(i);","handlingStrategy":"validation","validationCode":"uint count = lightBaker.albedoTextureCount;\nif (index < count)\n    var tex = lightBaker.GetAlbedoTexture(index);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Always read albedoTextureCount immediately before indexing.","Re-query counts after each bake; never cache across bakes.","Use strict < comparisons; never <= for uint index loops."],"tags":["gi","lightbaking","validation","index","argument"],"backgroundTag":null,"analyzedSha":"225b0fbdb57cc17d094e8056b71f8314aba56f73","analyzedAt":"2026-08-13T19:07:19.849Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}