{"record":{"id":"b65d6690ff63f3a2","repo":"Unity-Technologies/UnityCsReference","slug":"gameobject-is-already-part-of-target-prefab","errorCode":null,"errorMessage":"GameObject is already part of target prefab","messagePattern":"GameObject is already part of target prefab","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"Editor/Mono/Prefabs/PrefabUtility.bindings.cs","lineNumber":320,"sourceCode":"                {\n                    targetPrefabInstance = parentPrefabInstance;\n                    if (!IsPrefabInstanceObjectOf(go.transform.parent, targetPrefabObject))\n                        throw new ArgumentException(\"GameObject is not parented under a target Prefab instance.\");\n                }\n                else\n                {\n                    if (parentPrefabInstance != targetPrefabInstance)\n                    {\n                        throw new ArgumentException(\"GameObjects must be parented under the same Prefab instance.\");\n                    }\n                }\n\n                if (PrefabUtility.IsPartOfNonAssetPrefabInstance(go))\n                {\n                    var correspondingGO = PrefabUtility.GetCorrespondingObjectFromSource(go);\n                    var correspondingGOPrefabObject = PrefabUtility.GetPrefabAssetHandle(correspondingGO);\n                    if (targetPrefabObject == correspondingGOPrefabObject)\n                        throw new ArgumentException(\"GameObject is already part of target prefab\");\n                }\n            }\n\n            string prefabGUID = AssetDatabase.AssetPathToGUID(AssetDatabase.GetAssetPath(targetPrefab));\n            if (!VerifyNestingFromScript(gameObjects, prefabGUID, null))\n                throw new ArgumentException(\"Cyclic nesting detected\");\n\n            AddGameObjectsToPrefabAndConnect_Internal(gameObjects, targetPrefab);\n        }\n\n        [NativeMethod(\"AddGameObjectsToPrefabAndConnect\", IsFreeFunction = true)]\n        extern private static void AddGameObjectsToPrefabAndConnect_Internal([NotNull] GameObject[] gameObjects, [NotNull] Object prefab);\n\n        [NativeMethod(\"VerifyNestingFromScript\", IsFreeFunction = true)]\n        extern private static bool VerifyNestingFromScript([NotNull] GameObject[] gameObjects, [NotNull] string targetPrefabGUID, Object prefabInstance);\n\n        [StaticAccessor(\"PrefabUtilityBindings\", StaticAccessorType.DoubleColon)]\n        [NativeMethod(ThrowsException = true)]","sourceCodeStart":302,"sourceCodeEnd":338,"githubUrl":"https://github.com/Unity-Technologies/UnityCsReference/blob/225b0fbdb57cc17d094e8056b71f8314aba56f73/Editor/Mono/Prefabs/PrefabUtility.bindings.cs#L302-L338","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Skip objects whose corresponding source prefab equals the target before calling.","Track already-connected state and exclude those GameObjects.","Make your tool idempotent: filter out GetCorrespondingObjectFromSource(go) matches."],"exampleFix":"// before\nPrefabUtility.AddGameObjectsToPrefabAndConnect(gos, target); // some already connected\n\n// after\nvar targetHandle = PrefabUtility.GetPrefabAssetHandle(target);\ngos = gos.Where(g =>\n    PrefabUtility.GetPrefabAssetHandle(\n        PrefabUtility.GetCorrespondingObjectFromSource(g)) != targetHandle).ToArray();\nif (gos.Length > 0)\n    PrefabUtility.AddGameObjectsToPrefabAndConnect(gos, target);","handlingStrategy":"validation","validationCode":"var targetHandle = PrefabUtility.GetPrefabAssetHandle(targetPrefab);\ngameObjects = gameObjects.Where(g =>\n    PrefabUtility.GetPrefabAssetHandle(\n        PrefabUtility.GetCorrespondingObjectFromSource(g)) != targetHandle).ToArray();","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Make connect-tools idempotent by filtering out already-connected objects.","Track connection state so re-running a tool does not re-submit connected objects.","Compare prefab handles (not just names) when deduplicating."],"tags":["prefab","add-to-prefab","duplicate","idempotency"],"backgroundTag":null,"analyzedSha":"225b0fbdb57cc17d094e8056b71f8314aba56f73","analyzedAt":"2026-08-13T19:07:19.849Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}