Unity-Technologies/UnityCsReference · error · ArgumentException

Array is invalid.

Error message

Array is invalid.

What it means

Thrown by HasSameAssetPath when the input infos array is null or has zero elements. The method cannot determine a common asset path without at least one entry, so it rejects the input as invalid rather than returning a misleading result.

Source

Thrown at Editor/Mono/GUI/TargetChoiceHandler.cs:151

            }

            if (!PrefabUtility.HasSameParent(gameObjects))
                throw new ArgumentException(nameof(gameObjects), "ApplyPrefabAddedGameObjects requires that GameObjects share the same parent.");

            if (!HasSameAssetPath(infos))
                throw new ArgumentException(nameof(infos), "ApplyPrefabAddedGameObjects requires that GameObjects share the same parent asset path.");

            if (!PrefabUtility.PromptAndCheckoutPrefabIfNeeded(infos[0].assetPath, PrefabUtility.SaveVerb.Apply))
                return;

            PrefabUtility.ApplyAddedGameObjects(gameObjects, infos[0].assetPath, InteractionMode.UserAction);
            EditorUtility.ForceRebuildInspectors();
        }

        internal static bool HasSameAssetPath(ObjectInstanceAndSourcePathInfo[] infos)
        {
            if (infos == null || infos.Length == 0)
                throw new ArgumentException(nameof(infos), "Array is invalid.");

            string assetPath = infos[0].assetPath;
            for (int i = 1; i < infos.Length; i++)
            {
                if (assetPath != infos[i].assetPath)
                    return false;
            }

            return true;
        }

        internal static void RevertPrefabAddedGameObjects(object userData)
        {
            GameObject[] gameObjects = (GameObject[])userData;

            foreach (GameObject go in gameObjects)
                PrefabUtility.RevertAddedGameObject(go, InteractionMode.UserAction);

View on GitHub (pinned to 225b0fbdb5)

Solutions

  1. Guard callers: return early (no-op) when the selection is empty or null before reaching HasSameAssetPath.
  2. Initialize infos from a validated non-empty selection.
  3. Assert the count > 0 at the UI boundary and disable the apply action for empty selections.

Example fix

// before
if (!HasSameAssetPath(infos)) { /* ... */ } // infos may be empty

// after
if (infos == null || infos.Length == 0) return;
if (!HasSameAssetPath(infos)) { /* ... */ }
Defensive patterns

Strategy: validation

Validate before calling

if (infos == null || infos.Length == 0) { /* skip, nothing to do */ return; }

Prevention

When it happens

Trigger: Calling HasSameAssetPath with a null array or an empty ObjectInstanceAndSourcePathInfo[]; an upstream selection that produced no items being forwarded unfiltered.

Common situations: Empty user selection passed through to apply logic; a filtering step that removed all entries but did not short-circuit before validation.

Related errors


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