Unity-Technologies/UnityCsReference · error · ArgumentException

ApplyAddedGameObjects requires that GameObjects share the sa

Error message

ApplyAddedGameObjects requires that GameObjects share the same parent.

What it means

When applying multiple GameObjects at once, all of them must share the same parent transform so they can be applied into the same target location in the source prefab. The check HasSameParent enforces this precondition before performing the batched apply.

Source

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

            if (gameObjects == null)
                throw new ArgumentNullException(nameof(gameObjects), "Cannot apply added GameObjects. GameObjects array is null.");

            if (gameObjects.Length == 0)
                throw new ArgumentException(nameof(gameObjects), "No GameObjects in array.");

            foreach (GameObject go in gameObjects)
            {
                if (go == null)
                    throw new ArgumentException(nameof(go), "Input GameObject is null.");

                if (!IsAddedGameObjectOverride(go))
                    throw new ArgumentException(nameof(go), $"Cannot apply added GameObject. GameObject '{go.name}' is not an added GameObject override on a Prefab instance.");

                ThrowExceptionIfInstanceIsPersistent(go);
            }

            if (gameObjects.Length > 1 && !HasSameParent(gameObjects))
                throw new ArgumentException(nameof(gameObjects), "ApplyAddedGameObjects requires that GameObjects share the same parent.");

            GameObject gameObject = gameObjects[0];
            Transform instanceParent = gameObject.transform.parent;
            if (instanceParent == null)
                return;

            GameObject prefabSourceGameObjectParent = GetCorrespondingObjectFromSourceAtPath(instanceParent.gameObject, assetPath);
            if (prefabSourceGameObjectParent == null)
                return;

            var instanceRoot = GetOutermostPrefabInstanceRoot(instanceParent);
            if (instanceRoot == null)
                return;

            var sourceRoot = prefabSourceGameObjectParent.transform.root.gameObject;
            byte[] originalFileContent = null;

            PrefabUtility.Internal_CallPrefabInstanceApplying(instanceRoot);

View on GitHub (pinned to 225b0fbdb5)

Solutions

  1. Group GameObjects by parent: foreach (var grp in gos.GroupBy(g => g.transform.parent)) ApplyAddedGameObjects(grp.ToArray(), path, mode);
  2. Ensure all selected additions are siblings before submitting the batch.
  3. Fall back to ApplyAddedGameObject per object when parents differ.

Example fix

// before
PrefabUtility.ApplyAddedGameObjects(mixedParentGos, path, mode);

// after
foreach (var grp in mixedParentGos.GroupBy(g => g.transform.parent))
    PrefabUtility.ApplyAddedGameObjects(grp.ToArray(), path, mode);
Defensive patterns

Strategy: validation

Validate before calling

foreach (var grp in gameObjects.GroupBy(g => g.transform.parent))
{
    var arr = grp.ToArray();
    if (arr.Length > 0)
        PrefabUtility.ApplyAddedGameObjects(arr, assetPath, action);
}

Type guard

static bool ShareSameParent(GameObject[] gos) =>
    gos != null && gos.Length > 0 && gos.All(g => g != null
        && g.transform.parent == gos[0].transform.parent);

Prevention

When it happens

Trigger: Passing an array whose elements have different transform parents (different siblings groups, different hierarchy depths).

Common situations: Combining a multi-selection spanning multiple parts of the hierarchy into a single ApplyAddedGameObjects call; user-selected additions from unrelated parents.

Related errors


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