Unity-Technologies/UnityCsReference · error · InvalidOperationException

Attempting to assign an incorrect amount of layers to an Pla

Error message

Attempting to assign an incorrect amount of layers to an PlatformIcon, trying to assign {textures.Length} textures while the Icon requires atleast {minLayerCount} but no more than {maxLayerCount} layers

What it means

Thrown by PlatformIcon.SetTextures(params Texture2D[] textures) when the number of textures provided falls outside the icon's valid layer range [minLayerCount, maxLayerCount]. Each platform icon kind has a fixed required layer count, so assigning the wrong number of textures is a contract violation. Note: if all textures are null or the array is empty, the method silently clears instead of throwing.

Source

Thrown at Editor/Mono/PlatformSupport/PlayerSettingsPlatformIcons.bindings.cs:179

            if (layer > m_Textures.Count - 1)
            {
                for (int i = m_Textures.Count; i <= layer; i++)
                    m_Textures.Add(null);
            }

            m_Textures[layer] = texture;
        }

        public void SetTextures(params Texture2D[] textures)
        {
            if (textures == null || textures.Length == 0 || textures.TrueForAll(t => t == null))
            {
                m_Textures.Clear();
                return;
            }
            else if (textures.Length > maxLayerCount || textures.Length < minLayerCount)
            {
                throw new InvalidOperationException($"Attempting to assign an incorrect amount of layers to an PlatformIcon, trying to assign {textures.Length} textures while the Icon requires atleast {minLayerCount} but no more than {maxLayerCount} layers");
            }

#pragma warning disable UA2001 // The Banned API Analyzer produces compile errors for any new Linq code. This pre-existing usage has been suppressed, but should be rewritten if possible.
            m_Textures = textures.ToList();
#pragma warning restore UA2001
        }

        internal int GetValidLayerCount()
        {
#pragma warning disable UA2001 // The Banned API Analyzer produces compile errors for any new Linq code. This pre-existing usage has been suppressed, but should be rewritten if possible.
            var validLayerCount = m_Textures.Count(t => t != null);
#pragma warning restore UA2001
#pragma warning disable UA2001 // The Banned API Analyzer produces compile errors for any new Linq code. This pre-existing usage has been suppressed, but should be rewritten if possible.
            var previewTexturesCount = GetPreviewTextures().Count(t => t != null);
#pragma warning restore UA2001

            return Math.Max(previewTexturesCount, validLayerCount);
        }

View on GitHub (pinned to 225b0fbdb5)

Solutions

  1. Check icon.minLayerCount and icon.maxLayerCount before calling SetTextures, and ensure your array length is within that range.
  2. Pad or trim the texture array to the required count: Array.Resize(ref textures, icon.maxLayerCount).
  3. Query the platform's icon kind specifications via GetIconsForPublishing or similar API to discover expected layer counts before assignment.

Example fix

// before
icon.SetTextures(textures); // textures.Length doesn't match

// after
if (textures.Length >= icon.minLayerCount && textures.Length <= icon.maxLayerCount)
    icon.SetTextures(textures);
Defensive patterns

Strategy: validation

Validate before calling

if (textures.Length >= icon.minLayerCount && textures.Length <= icon.maxLayerCount)
    icon.SetTextures(textures);

Prevention

When it happens

Trigger: Calling SetTextures with an array whose Length is less than minLayerCount or greater than maxLayerCount for that specific icon kind. Commonly occurs when fetching icons from one build target and applying them to another with a different layer requirement, or when deserializing icon data that doesn't match the target platform spec.

Common situations: Cross-platform icon automation scripts; batch-importing icon sets; mismatch between the icon kind selected and the number of textures in the source asset; upgrading a project to a Unity version where a platform's icon layer count changed.

Related errors


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