Unity-Technologies/UnityCsReference · error · InvalidOperationException

Attempting to set an incorrect number of icons for {buildTar

Error message

Attempting to set an incorrect number of icons for {buildTarget.TargetName} {kind} kind, it requires {requiredIconCount} icons but trying to assign {icons.Length}.

What it means

Thrown when setting platform icons for a specific build target and icon kind, and the number of PlatformIcon objects provided does not equal the required count returned by PlatformIcon.GetRequiredPlatformIconsByType. Each (buildTarget, kind) pair has a fixed required number of icons, and this method enforces that the array length matches exactly before forwarding to the native SetPlatformIconsInternal.

Source

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

        public static void SetPlatformIcons(NamedBuildTarget buildTarget, PlatformIconKind kind, PlatformIcon[] icons)
        {
            if (!BuildTargetDiscovery.TryGetBuildTarget(BuildPipeline.GetBuildTargetByName(buildTarget.TargetName), out var iBuildTarget))
                return;

            var requiredIcons = iBuildTarget.IconPlatformProperties?.GetRequiredPlatformIcons();
            if (requiredIcons == null)
                return;

            var requiredIconCount = PlatformIcon.GetRequiredPlatformIconsByType(kind, requiredIcons).Length;

            PlatformIconStruct[] iconStructs;
            if (icons == null)
            {
                iconStructs = Array.Empty<PlatformIconStruct>();
            }
            else if (requiredIconCount != icons.Length)
            {
                throw new InvalidOperationException($"Attempting to set an incorrect number of icons for {buildTarget.TargetName} {kind} kind, it requires {requiredIconCount} icons but trying to assign {icons.Length}.");
            }
            else
            {
                iconStructs = Array.ConvertAll(icons, i => i.GetPlatformIconStruct());
            }

            SetPlatformIconsInternal(buildTarget.TargetName, iconStructs, kind.kind);
        }

        [Obsolete("Use GetSupportedIconKinds(NamedBuildTarget) instead")]
        public static PlatformIconKind[] GetSupportedIconKindsForPlatform(BuildTargetGroup platform) =>
            GetSupportedIconKinds(NamedBuildTarget.FromBuildTargetGroup(platform));

        public static PlatformIconKind[] GetSupportedIconKinds(NamedBuildTarget buildTarget)
        {
            if (BuildTargetDiscovery.TryGetBuildTarget(BuildPipeline.GetBuildTargetByName(buildTarget.TargetName), out var iBuildTarget))
            {
                var requiredIcons = iBuildTarget.IconPlatformProperties?.GetRequiredPlatformIcons();

View on GitHub (pinned to 225b0fbdb5)

Solutions

  1. Query the required count first: var required = PlatformIcon.GetRequiredPlatformIconsByType(kind, availableIcons); and size your array to match before calling set.
  2. Fetch the correct icon set per build target/kind using GetIcons(NamedBuildTarget) or GetIconsForPublishing to get properly-sized arrays.
  3. Avoid reusing icon arrays across platforms — each (target, kind) pair may require a different number of icons.

Example fix

// before
SetPlatformIcons(buildTarget, kind, myIcons); // length mismatch

// after
var requiredCount = PlatformIcon.GetRequiredPlatformIconsByType(kind, availableIcons).Length;
if (myIcons.Length == requiredCount)
    SetPlatformIcons(buildTarget, kind, myIcons);
Defensive patterns

Strategy: validation

Validate before calling

var requiredCount = PlatformIcon.GetRequiredPlatformIconsByType(kind, availableIcons).Length;
if (icons != null && icons.Length == requiredCount)
    SetPlatformIcons(buildTarget, kind, icons);

Prevention

When it happens

Trigger: Calling SetPlatformIcons (or the property setter that invokes it) with an icons array whose Length != requiredIconCount for that build target and kind. Occurs when the caller builds the icon array for one platform/kind and applies it to a different one with a different required count.

Common situations: Editor scripts that bulk-configure icons across platforms; copying icon arrays between build targets; Unity version upgrades that change the required icon count for a platform; custom build pipelines that don't query the required count per kind.

Related errors


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