Unity-Technologies/UnityCsReference · error · ArgumentException

ApplyPrefabAddedGameObjects requires that GameObjects share

Error message

ApplyPrefabAddedGameObjects requires that GameObjects share the same parent.

What it means

Thrown by ApplyPrefabAddedGameObjects when applying added GameObjects back into a prefab asset but the supplied GameObjects do not all share the same parent Transform in the hierarchy. PrefabUtility.HasSameParent enforces that all objects belong under one common parent so the apply operation is unambiguous.

Source

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

        {
            ObjectInstanceAndSourceInfo info = (ObjectInstanceAndSourceInfo)userData;
            PrefabUtility.RevertRemovedComponent((GameObject)info.instanceObject, (Component)info.correspondingObjectInSource, InteractionMode.UserAction);
            EditorUtility.ForceRebuildInspectors();
        }

        internal static void ApplyPrefabAddedGameObjects(object userData)
        {
            ObjectInstanceAndSourcePathInfo[] infos = (ObjectInstanceAndSourcePathInfo[])userData;

            GameObject[] gameObjects = new GameObject[infos.Length];
            for (int i = 0; i < infos.Length; i++)
            {
                ObjectInstanceAndSourcePathInfo info = infos[i];
                gameObjects[i] = info.instanceObject as GameObject;
            }

            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++)

View on GitHub (pinned to 225b0fbdb5)

Solutions

  1. Group the selection by parent transform before calling ApplyPrefabAddedGameObjects, applying each parent-group separately.
  2. Re-parent the added GameObjects so they share a common parent before invoking apply.
  3. Filter the selection to only objects under the same parent and warn the user about excluded siblings.

Example fix

// before
ApplyPrefabAddedGameObjects(infos); // mixed parents

// after
foreach (var group in infos.GroupBy(i => ((GameObject)i.instanceObject).transform.parent))
    ApplyPrefabAddedGameObjects(group.ToArray());
Defensive patterns

Strategy: validation

Validate before calling

bool SameParent(GameObject[] gos) => gos.All(g => g.transform.parent == gos[0].transform.parent);

Prevention

When it happens

Trigger: Invoking 'Apply Added GameObjects' on a multi-selection of GameObjects that live under different parent transforms; calling ApplyPrefabAddedGameObjects programmatically with a mixed-parent ObjectInstanceAndSourcePathInfo[] array.

Common situations: User multi-selects added objects scattered across different parents in a prefab instance and chooses Apply; a custom batch script groups added objects by asset path but not by hierarchy parent.

Related errors


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