Unity-Technologies/UnityCsReference · error · ArgumentOutOfRangeException

texIndex must point to a valid index in textureData list.

Error message

texIndex must point to a valid index in textureData list.

What it means

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.

Source

Thrown at Editor/Mono/2D/SpriteAtlas/EditorSpritePacking.bindings.cs:121

        extern internal static GUID GetActivePreviewAtlasGUID();

        internal unsafe static SpritePackDataset PackCustomSpritesWrapper(SpritePackDataset input, SpritePackConfig packConfig, Allocator alloc)
        {
            var output = new SpritePackDataset();
            var spriteCount = input.spriteData.Count;
            if (0 == spriteCount)
                return output;

            var data = new NativeArray<SpritePackDatasetInternal>(spriteCount, Allocator.Temp, NativeArrayOptions.ClearMemory);
            for (int i = 0; i < spriteCount; ++i)
            {
                SpritePackDatasetInternal rawData = data[i];
                rawData.spriteData.guid = input.spriteData[i].guid;
                int texIndex = input.spriteData[i].texIndex;
                if (texIndex >= input.textureData.Count)
                {
                    data.Dispose();
                    throw new ArgumentOutOfRangeException("texIndex", "texIndex must point to a valid index in textureData list.");
                }
                rawData.spriteData.texIndex = texIndex;
                rawData.spriteData.indexCount = input.spriteData[i].indexCount;
                rawData.spriteData.vertexCount = input.spriteData[i].vertexCount;
                rawData.spriteData.rect = input.spriteData[i].rect;
                rawData.spriteData.indices = input.spriteData[i].indices.IsCreated ? (IntPtr)input.spriteData[i].indices.GetUnsafePtr() : (IntPtr)0;
                rawData.spriteData.vertices = input.spriteData[i].vertices.IsCreated  ? (IntPtr)input.spriteData[i].vertices.GetUnsafePtr() : (IntPtr)0;
                rawData.textureData.width = input.textureData[texIndex].width;
                rawData.textureData.height = input.textureData[texIndex].height;
                rawData.textureData.buffer = input.textureData[texIndex].buffer.IsCreated ? (IntPtr)input.textureData[texIndex].buffer.GetUnsafePtr() : (IntPtr)0;
                data[i] = rawData;
            }

            var spriteOutput = (SpritePackDatasetInternal*)PackCustomSpritesInternal(spriteCount, (SpritePackDatasetInternal*)data.GetUnsafePtr(), packConfig);
            if (null != spriteOutput)
            {
                var colorBufferArray = new SpritePackTextureInfo[spriteCount];
                for (int i = 0; i < spriteCount; ++i)

View on GitHub (pinned to 225b0fbdb5)

Solutions

  1. Reimport the SpriteAtlas asset (right-click > Reimport) so textureData and indices are rebuilt.
  2. Remove deleted/missing source textures from the atlas Objects/Packables list and repack.
  3. If generating pack input programmatically, assert spriteData[i].texIndex < textureData.Count for every i before submission.
  4. Upgrade the project in a clean library and reimport all 2D assets if the error appeared after a Unity version bump.

Example fix

// before (programmatic pack input)
rawData.spriteData.texIndex = cachedTexIndex; // stale after a texture was removed

// after
if (cachedTexIndex >= textureData.Count) throw new InvalidOperationException($"texIndex {cachedTexIndex} out of range for {sprite.name}; rebuild input.");
rawData.spriteData.texIndex = cachedTexIndex;
Defensive patterns

Strategy: validation

Validate before calling

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}");

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


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