Unity-Technologies/UnityCsReference · error · ArgumentException

Array is invalid.

Error message

Array is invalid.

What it means

Internal helper HasSameParent throws ArgumentException when the array is null, empty, or its first element is null. These are preconditions needed before comparing parents; without a valid first element there is no baseline parent to compare against.

Source

Thrown at Editor/Mono/Prefabs/PrefabUtility.cs:1735

                Analytics.SendApplyEvent(
                    Analytics.ApplyScope.AddedGameObject,
                    instanceRoot,
                    assetPath,
                    action,
                    startTime,
                    false
                );
            }

            PrefabUtility.Internal_CallPrefabInstanceApplied(instanceRoot);
            EditorUtility.ForceRebuildInspectors();
        }

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

            Transform goParent = gameObjects[0].transform.parent;

            if (goParent == null)
                throw new ArgumentException(nameof(goParent), "Object is a parentless root.");

            foreach (GameObject go in gameObjects)
            {
                if (go.transform.parent != goParent)
                    return false;
            }

            return true;
        }

        private static void RemoveRemovedGameObjectOverride(Object instanceObject, GameObject assetGameObject)
        {
            var removedGameObjects = PrefabUtility.GetRemovedGameObjects(instanceObject);

View on GitHub (pinned to 225b0fbdb5)

Solutions

  1. Ensure callers validate the array (non-null, non-empty, first element non-null) before invoking; the public ApplyAddedGameObjects already does this, so reaching here suggests a bug.
  2. If reached through normal API usage, report it as a Unity bug with the reproduction steps.
  3. Avoid calling internal helpers directly; use the public API which performs the precondition checks.

Example fix

// before (internal misuse)
bool same = PrefabUtility.HasSameParent(maybeEmptyArray);

// after
if (maybeEmptyArray != null && maybeEmptyArray.Length > 0 && maybeEmptyArray[0] != null)
    bool same = PrefabUtility.HasSameParent(maybeEmptyArray);
Defensive patterns

Strategy: validation

Validate before calling

if (gameObjects == null || gameObjects.Length == 0 || gameObjects[0] == null)
    return false;

Type guard

static bool IsValidArrayForParentCheck(GameObject[] gos) =>
    gos != null && gos.Length > 0 && gos[0] != null;

Prevention

When it happens

Trigger: Reached internally by ApplyAddedGameObjects with a malformed array, or called directly with null/empty array or null first element. The public API normally guards these earlier, so reaching this throw indicates internal misuse or an unexpected call path.

Common situations: An upstream guard was bypassed; reflection or internal invocation called HasSameParent without the normal precondition checks; a bug in calling code passing an unfiltered array.

Related errors


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