Unity-Technologies/UnityCsReference · error · ArgumentOutOfRangeException

Attempting to set icon layer

Error message

Attempting to set icon layer {layer}, while icon only supports {maxLayerCount} layers!

What it means

Thrown by PlatformIcon.SetTexture(Texture2D texture, int layer) when the layer index is negative or exceeds maxLayerCount. Unlike GetTexture, SetTexture will auto-expand the internal m_Textures list up to maxLayerCount by padding with nulls, but it still rejects indices outside the valid range defined by the icon's kind description.

Solutions

  1. Validate against icon.maxLayerCount before calling SetTexture.
  2. Use SetTextures(params Texture2D[]) to assign all layers at once after validating the array length falls within [minLayerCount, maxLayerCount].
  3. When iterating, clamp or skip layers beyond capacity: for (int i = 0; i < Mathf.Min(textures.Length, icon.maxLayerCount); i++).

Example fix

// before
icon.SetTexture(myTexture, 5); // icon.maxLayerCount is 3

// after
if (5 < icon.maxLayerCount)
    icon.SetTexture(myTexture, 5);
Defensive patterns

Strategy: validation

Validate before calling

if (layer >= 0 && layer < icon.maxLayerCount)
    icon.SetTexture(texture, layer);

Prevention

When it happens

Trigger: Calling SetTexture with layer >= maxLayerCount or layer < 0. Happens when programmatically assigning textures to icon layers using indices derived from a different icon kind's layer layout, or when incrementing layer counters past the platform maximum.

Common situations: Bulk-setting icons across platforms with varying layer counts; editor scripts that assign textures in a loop without checking per-icon capacity; importing icon configuration from a format that doesn't match the target platform's layer schema.

Related errors


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

Appendix: source

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

        {
            return m_Textures.ToArray();
        }

        internal void SetPreviewTextures(Texture2D[] textures)
        {
            m_PreviewTextures = textures;
        }

        internal Texture2D[] GetPreviewTextures()
        {
            return m_PreviewTextures;
        }

        public void SetTexture(Texture2D texture, int layer = 0)
        {
            if (layer < 0 || layer >= maxLayerCount)
            {
                throw new ArgumentOutOfRangeException($"Attempting to set icon layer {layer}, while icon only supports {maxLayerCount} layers!");
            }
            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)

View on GitHub (pinned to 225b0fbdb5)