{"record":{"id":"995edd8739c459e0","repo":"Unity-Technologies/UnityCsReference","slug":"gameobjects-must-be-parented-under-the-same-prefab","errorCode":null,"errorMessage":"GameObjects must be parented under the same Prefab instance.","messagePattern":"GameObjects must be parented under the same Prefab instance\\.","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"Editor/Mono/Prefabs/PrefabUtility.bindings.cs","lineNumber":311,"sourceCode":"\n                if (EditorUtility.IsPersistent(go))  // Prefab asset\n                    throw new ArgumentException(\"Game object is part of a prefab\");\n\n                var parentPrefabInstance = GetParentPrefabInstance(go);\n                if (parentPrefabInstance == null)\n                    throw new ArgumentException(\"GameObject is not (directly) parented under a target Prefab instance.\");\n\n                if (targetPrefabInstance == null)\n                {\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        }","sourceCodeStart":293,"sourceCodeEnd":329,"githubUrl":"https://github.com/Unity-Technologies/UnityCsReference/blob/225b0fbdb57cc17d094e8056b71f8314aba56f73/Editor/Mono/Prefabs/PrefabUtility.bindings.cs#L293-L329","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Group the GameObjects by their parent prefab instance and call the API once per group.","Verify each go shares the same GetParentPrefabInstance result as the first before calling.","Reduce the selection to a single instance's children."],"exampleFix":"// before\nGameObject[] gos = selectionFromManyInstances;\nPrefabUtility.AddGameObjectsToPrefabAndConnect(gos, target); // mixed\n\n// after\nforeach (var group in gos.GroupBy(g => PrefabUtility.GetOutermostPrefabInstanceRoot(g)))\n{\n    if (group.Key == null) continue;\n    PrefabUtility.AddGameObjectsToPrefabAndConnect(group.ToArray(), target);\n}","handlingStrategy":"validation","validationCode":"foreach (var group in gameObjects\n    .GroupBy(g => PrefabUtility.GetOutermostPrefabInstanceRoot(g)))\n{\n    if (group.Key == null) continue;\n    PrefabUtility.AddGameObjectsToPrefabAndConnect(group.ToArray(), target);\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Group objects by their parent prefab instance and call the API once per group.","Pre-check that all objects share one GetParentPrefabInstance result before calling.","Limit selections to a single instance in the tool's UI."],"tags":["prefab","add-to-prefab","instance-mismatch","batch"],"backgroundTag":null,"analyzedSha":"225b0fbdb57cc17d094e8056b71f8314aba56f73","analyzedAt":"2026-08-13T19:07:19.849Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}