Unity-Technologies/UnityCsReference · error · ArgumentException

GameObjects must be parented under the same Prefab instance.

Error message

GameObjects must be parented under the same Prefab instance.

What it means

Thrown for subsequent GameObjects (after the first sets targetPrefabInstance) when that object's parentPrefabInstance differs from the one recorded. All objects must be parented under the SAME prefab instance to be connected together in one call.

Source

Thrown at Editor/Mono/Prefabs/PrefabUtility.bindings.cs:311

                if (EditorUtility.IsPersistent(go))  // Prefab asset
                    throw new ArgumentException("Game object is part of a prefab");

                var parentPrefabInstance = GetParentPrefabInstance(go);
                if (parentPrefabInstance == null)
                    throw new ArgumentException("GameObject is not (directly) parented under a target Prefab instance.");

                if (targetPrefabInstance == null)
                {
                    targetPrefabInstance = parentPrefabInstance;
                    if (!IsPrefabInstanceObjectOf(go.transform.parent, targetPrefabObject))
                        throw new ArgumentException("GameObject is not parented under a target Prefab instance.");
                }
                else
                {
                    if (parentPrefabInstance != targetPrefabInstance)
                    {
                        throw new ArgumentException("GameObjects must be parented under the same Prefab instance.");
                    }
                }

                if (PrefabUtility.IsPartOfNonAssetPrefabInstance(go))
                {
                    var correspondingGO = PrefabUtility.GetCorrespondingObjectFromSource(go);
                    var correspondingGOPrefabObject = PrefabUtility.GetPrefabAssetHandle(correspondingGO);
                    if (targetPrefabObject == correspondingGOPrefabObject)
                        throw new ArgumentException("GameObject is already part of target prefab");
                }
            }

            string prefabGUID = AssetDatabase.AssetPathToGUID(AssetDatabase.GetAssetPath(targetPrefab));
            if (!VerifyNestingFromScript(gameObjects, prefabGUID, null))
                throw new ArgumentException("Cyclic nesting detected");

            AddGameObjectsToPrefabAndConnect_Internal(gameObjects, targetPrefab);
        }

View on GitHub (pinned to 225b0fbdb5)

Solutions

  1. Group the GameObjects by their parent prefab instance and call the API once per group.
  2. Verify each go shares the same GetParentPrefabInstance result as the first before calling.
  3. Reduce the selection to a single instance's children.

Example fix

// before
GameObject[] gos = selectionFromManyInstances;
PrefabUtility.AddGameObjectsToPrefabAndConnect(gos, target); // mixed

// after
foreach (var group in gos.GroupBy(g => PrefabUtility.GetOutermostPrefabInstanceRoot(g)))
{
    if (group.Key == null) continue;
    PrefabUtility.AddGameObjectsToPrefabAndConnect(group.ToArray(), target);
}
Defensive patterns

Strategy: validation

Validate before calling

foreach (var group in gameObjects
    .GroupBy(g => PrefabUtility.GetOutermostPrefabInstanceRoot(g)))
{
    if (group.Key == null) continue;
    PrefabUtility.AddGameObjectsToPrefabAndConnect(group.ToArray(), target);
}

Prevention

When it happens

Trigger: Passing objects drawn from two or more distinct prefab instances in a single array. The first object sets the canonical instance; any object under a different instance throws.

Common situations: Multi-selecting across multiple prefab instances in the scene. Combining children of prefab instance A and instance B (even if both are instances of the same asset) into one call.

Related errors


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