Unity-Technologies/UnityCsReference · error · ArgumentException

GameObject is already part of target prefab

Error message

GameObject is already part of target prefab

What it means

Thrown when the GameObject is part of a non-asset prefab instance AND its corresponding source object's prefab handle equals the target prefab handle. In other words, the object is already connected to / part of the target prefab, so re-connecting is a no-op error.

Source

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

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

        [NativeMethod("AddGameObjectsToPrefabAndConnect", IsFreeFunction = true)]
        extern private static void AddGameObjectsToPrefabAndConnect_Internal([NotNull] GameObject[] gameObjects, [NotNull] Object prefab);

        [NativeMethod("VerifyNestingFromScript", IsFreeFunction = true)]
        extern private static bool VerifyNestingFromScript([NotNull] GameObject[] gameObjects, [NotNull] string targetPrefabGUID, Object prefabInstance);

        [StaticAccessor("PrefabUtilityBindings", StaticAccessorType.DoubleColon)]
        [NativeMethod(ThrowsException = true)]

View on GitHub (pinned to 225b0fbdb5)

Solutions

  1. Skip objects whose corresponding source prefab equals the target before calling.
  2. Track already-connected state and exclude those GameObjects.
  3. Make your tool idempotent: filter out GetCorrespondingObjectFromSource(go) matches.

Example fix

// before
PrefabUtility.AddGameObjectsToPrefabAndConnect(gos, target); // some already connected

// after
var targetHandle = PrefabUtility.GetPrefabAssetHandle(target);
gos = gos.Where(g =>
    PrefabUtility.GetPrefabAssetHandle(
        PrefabUtility.GetCorrespondingObjectFromSource(g)) != targetHandle).ToArray();
if (gos.Length > 0)
    PrefabUtility.AddGameObjectsToPrefabAndConnect(gos, target);
Defensive patterns

Strategy: validation

Validate before calling

var targetHandle = PrefabUtility.GetPrefabAssetHandle(targetPrefab);
gameObjects = gameObjects.Where(g =>
    PrefabUtility.GetPrefabAssetHandle(
        PrefabUtility.GetCorrespondingObjectFromSource(g)) != targetHandle).ToArray();

Prevention

When it happens

Trigger: Passing a GameObject whose GetCorrespondingObjectFromSource already maps to the target prefab. The object was previously connected or is inherently a child of the target's instance.

Common situations: Re-running a connect operation on already-connected objects. Selecting objects that are already children of the target prefab instance. Idempotency not handled in a custom tool.

Related errors


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