Unity-Technologies/UnityCsReference · error · ArgumentException

GameObject is not parented under a target Prefab instance.

Error message

GameObject is not parented under a target Prefab instance.

What it means

Thrown on the FIRST processed GameObject: after capturing targetPrefabInstance from its parent, Unity checks IsPrefabInstanceObjectOf(go.transform.parent, targetPrefabObject). If the parent instance is not derived from the target prefab asset, the object is considered under the wrong instance.

Source

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

            var targetPrefabObject = PrefabUtility.GetPrefabAssetHandle(targetPrefab);

            foreach (GameObject go in gameObjects)
            {
                if (go == null)
                    throw new ArgumentException("GameObject in input 'gameObjects' array is null");

                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");
                }
            }

View on GitHub (pinned to 225b0fbdb5)

Solutions

  1. Ensure the selected GameObject's parent prefab instance is an instance of `targetPrefab`.
  2. Resolve the target prefab from the first selected object's own instance to guarantee a match.
  3. Validate IsPrefabInstanceObjectOf(go.transform.parent, targetHandle) before calling.

Example fix

// before
GameObject[] gos = FindObjectsUnderPrefabA();
PrefabUtility.AddGameObjectsToPrefabAndConnect(gos, prefabB); // mismatch

// after
GameObject[] gos = FindObjectsUnderPrefabA();
var targetHandle = PrefabUtility.GetPrefabAssetHandle(prefabA);
// confirm parents match targetHandle, then:
PrefabUtility.AddGameObjectsToPrefabAndConnect(gos, prefabA);
Defensive patterns

Strategy: validation

Validate before calling

var targetHandle = PrefabUtility.GetPrefabAssetHandle(targetPrefab);
bool ok = gameObjects.All(g =>
    IsPrefabInstanceObjectOf(g.transform.parent, targetHandle));
if (!ok) throw new ArgumentException("Objects must be under an instance of the target prefab.");

Prevention

When it happens

Trigger: The GameObject is parented under a different prefab instance (a different prefab asset) than `targetPrefab`. Combining objects from prefab A's instance while trying to connect them into prefab B.

Common situations: Mismatched selection: user selected objects under one prefab but specified a different target prefab. Script wiring the wrong target to a selection sourced from another prefab.

Related errors


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