Unity-Technologies/UnityCsReference · error · ArgumentOutOfRangeException
Attempting to retrieve icon layer {layer}, while the icon on
Error message
Attempting to retrieve icon layer {layer}, while the icon only contains {layerCount} layers! What it means
Thrown by PlatformIcon.GetTexture(int layer) when the requested layer index is negative or greater-than-or-equal to maxLayerCount. The icon's layer capacity is fixed by its description (minLayerCount to maxLayerCount range), so requesting a layer beyond that capacity is an out-of-bounds access. Note: the message uses layerCount (currently populated textures) but the guard checks against maxLayerCount.
Source
Thrown at Editor/Mono/PlatformSupport/PlayerSettingsPlatformIcons.bindings.cs:136
internal PlatformIcon(int width, int height, int minLayerCount, int maxLayerCount, string iconSubKind, string description, PlatformIconKind kind, bool draggable = true)
{
this.width = width;
this.height = height;
this.iconSubKind = iconSubKind;
this.description = description;
this.minLayerCount = minLayerCount;
this.maxLayerCount = maxLayerCount;
this.kind = kind;
this.draggable = draggable;
m_Textures = new List<Texture2D>();
}
public Texture2D GetTexture(int layer = 0)
{
if (layer < 0 || layer >= maxLayerCount)
throw new ArgumentOutOfRangeException($"Attempting to retrieve icon layer {layer}, while the icon only contains {layerCount} layers!");
return layer < layerCount ? m_Textures[layer] : null;
}
public Texture2D[] GetTextures()
{
return m_Textures.ToArray();
}
internal void SetPreviewTextures(Texture2D[] textures)
{
m_PreviewTextures = textures;
}
internal Texture2D[] GetPreviewTextures()
{
return m_PreviewTextures;
}
View on GitHub (pinned to 225b0fbdb5)
Solutions
- Check icon.maxLayerCount before accessing a specific layer: if (layer >= 0 && layer < icon.maxLayerCount).
- Use GetTextures() to retrieve all textures as an array when you need to iterate without index assumptions.
- Query GetValidLayerCount() to know how many layers are actually populated before indexing.
Example fix
// before var tex = icon.GetTexture(3); // icon only supports 2 layers // after var tex = (3 < icon.maxLayerCount) ? icon.GetTexture(3) : null;
Defensive patterns
Strategy: validation
Validate before calling
if (layer < 0 || layer >= icon.maxLayerCount)
return null;
return icon.GetTexture(layer); Prevention
- Always check icon.maxLayerCount before indexing into icon layers.
- Use GetTextures() for iteration-free bulk access.
- Query GetValidLayerCount() to know how many textures are actually populated.
When it happens
Trigger: Calling GetTexture with layer >= maxLayerCount or layer < 0. Happens when iterating layers using an array length from a different icon kind, or when hard-coding a layer index that exceeds the platform's supported layer count for that icon kind.
Common situations: Setting platform icons programmatically across multiple build targets that have different layer counts; assuming all icon kinds have the same number of layers; copying texture-access code from one platform to another without adjusting layer bounds.
Related errors
- Attempting to set icon layer {layer}, while icon only suppor
- Attempting to assign an incorrect amount of layers to an Pla
- Attempting to set an incorrect number of icons for {buildTar
- The provided target platform group name ({buildTargetName})
- Value cannot be null. (Parameter 'defines')
AI-assisted analysis of Unity-Technologies/UnityCsReference@225b0fbdb5 (2026-08-13).
Data as JSON: /api/errors/7ee937c4813bc450.
Report an issue: GitHub.