{"record":{"id":"56c33123d5b9372d","repo":"Unity-Technologies/UnityCsReference","slug":"texindex-must-point-to-a-valid-index-in-texturedat","errorCode":null,"errorMessage":"texIndex must point to a valid index in textureData list.","messagePattern":"texIndex must point to a valid index in textureData list\\.","errorType":"validation","errorClass":"ArgumentOutOfRangeException","httpStatus":null,"severity":"error","filePath":"Editor/Mono/2D/SpriteAtlas/EditorSpritePacking.bindings.cs","lineNumber":121,"sourceCode":"        extern internal static GUID GetActivePreviewAtlasGUID();\n\n        internal unsafe static SpritePackDataset PackCustomSpritesWrapper(SpritePackDataset input, SpritePackConfig packConfig, Allocator alloc)\n        {\n            var output = new SpritePackDataset();\n            var spriteCount = input.spriteData.Count;\n            if (0 == spriteCount)\n                return output;\n\n            var data = new NativeArray<SpritePackDatasetInternal>(spriteCount, Allocator.Temp, NativeArrayOptions.ClearMemory);\n            for (int i = 0; i < spriteCount; ++i)\n            {\n                SpritePackDatasetInternal rawData = data[i];\n                rawData.spriteData.guid = input.spriteData[i].guid;\n                int texIndex = input.spriteData[i].texIndex;\n                if (texIndex >= input.textureData.Count)\n                {\n                    data.Dispose();\n                    throw new ArgumentOutOfRangeException(\"texIndex\", \"texIndex must point to a valid index in textureData list.\");\n                }\n                rawData.spriteData.texIndex = texIndex;\n                rawData.spriteData.indexCount = input.spriteData[i].indexCount;\n                rawData.spriteData.vertexCount = input.spriteData[i].vertexCount;\n                rawData.spriteData.rect = input.spriteData[i].rect;\n                rawData.spriteData.indices = input.spriteData[i].indices.IsCreated ? (IntPtr)input.spriteData[i].indices.GetUnsafePtr() : (IntPtr)0;\n                rawData.spriteData.vertices = input.spriteData[i].vertices.IsCreated  ? (IntPtr)input.spriteData[i].vertices.GetUnsafePtr() : (IntPtr)0;\n                rawData.textureData.width = input.textureData[texIndex].width;\n                rawData.textureData.height = input.textureData[texIndex].height;\n                rawData.textureData.buffer = input.textureData[texIndex].buffer.IsCreated ? (IntPtr)input.textureData[texIndex].buffer.GetUnsafePtr() : (IntPtr)0;\n                data[i] = rawData;\n            }\n\n            var spriteOutput = (SpritePackDatasetInternal*)PackCustomSpritesInternal(spriteCount, (SpritePackDatasetInternal*)data.GetUnsafePtr(), packConfig);\n            if (null != spriteOutput)\n            {\n                var colorBufferArray = new SpritePackTextureInfo[spriteCount];\n                for (int i = 0; i < spriteCount; ++i)","sourceCodeStart":103,"sourceCodeEnd":139,"githubUrl":"https://github.com/Unity-Technologies/UnityCsReference/blob/225b0fbdb57cc17d094e8056b71f8314aba56f73/Editor/Mono/2D/SpriteAtlas/EditorSpritePacking.bindings.cs#L103-L139","documentation":"Thrown inside the native interop marshaling of sprite pack data when a sprite's texIndex is >= the textureData list count. Each sprite references its source texture by index into input.textureData; an out-of-range index would read past the end of the buffer when copying width/height/pointer, so the binding disposes its temp NativeArray and throws ArgumentOutOfRangeException before the unsafe read.","triggerScenarios":"A SpriteAtlasImporter or custom packing input where spriteData[i].texIndex was set from stale data after the textureData list shrank. A deserialized SpriteAtlas whose textures were deleted but indices were not rebuilt. Programmatic construction of the internal pack dataset with mismatched sprite/texture arrays.","commonSituations":"Sprite atlas source textures removed from disk while the .spriteatlas asset still references them by index. Unity version upgrades that changed internal packing data layout, leaving texIndex inconsistent. Third-party or tool-generated atlas inputs that do not rebuild indices after editing textures.","solutions":["Reimport the SpriteAtlas asset (right-click > Reimport) so textureData and indices are rebuilt.","Remove deleted/missing source textures from the atlas Objects/Packables list and repack.","If generating pack input programmatically, assert spriteData[i].texIndex < textureData.Count for every i before submission.","Upgrade the project in a clean library and reimport all 2D assets if the error appeared after a Unity version bump."],"exampleFix":"// before (programmatic pack input)\nrawData.spriteData.texIndex = cachedTexIndex; // stale after a texture was removed\n\n// after\nif (cachedTexIndex >= textureData.Count) throw new InvalidOperationException($\"texIndex {cachedTexIndex} out of range for {sprite.name}; rebuild input.\");\nrawData.spriteData.texIndex = cachedTexIndex;","handlingStrategy":"validation","validationCode":"for (int i = 0; i < spriteData.Count; i++) if (spriteData[i].texIndex >= textureData.Count) throw new InvalidOperationException($\"texIndex {spriteData[i].texIndex} out of range at sprite {i}\");","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Reimport .spriteatlas assets after adding/removing source textures.","When constructing pack input programmatically, assert texIndex bounds for every sprite.","After a Unity upgrade, force a full reimport of 2D assets in a clean Library."],"tags":["sprite-atlas","argument-out-of-range","native-interop","asset-corruption","csharp"],"backgroundTag":null,"analyzedSha":"225b0fbdb57cc17d094e8056b71f8314aba56f73","analyzedAt":"2026-08-13T19:07:19.849Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}