{"record":{"id":"248c589ee64ebf6c","repo":"Unity-Technologies/UnityCsReference","slug":"applyaddedgameobjects-requires-that-gameobjects-sh","errorCode":null,"errorMessage":"ApplyAddedGameObjects requires that GameObjects share the same parent.","messagePattern":"ApplyAddedGameObjects requires that GameObjects share the same parent\\.","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"Editor/Mono/Prefabs/PrefabUtility.cs","lineNumber":1664,"sourceCode":"            if (gameObjects == null)\n                throw new ArgumentNullException(nameof(gameObjects), \"Cannot apply added GameObjects. GameObjects array is null.\");\n\n            if (gameObjects.Length == 0)\n                throw new ArgumentException(nameof(gameObjects), \"No GameObjects in array.\");\n\n            foreach (GameObject go in gameObjects)\n            {\n                if (go == null)\n                    throw new ArgumentException(nameof(go), \"Input GameObject is null.\");\n\n                if (!IsAddedGameObjectOverride(go))\n                    throw new ArgumentException(nameof(go), $\"Cannot apply added GameObject. GameObject '{go.name}' is not an added GameObject override on a Prefab instance.\");\n\n                ThrowExceptionIfInstanceIsPersistent(go);\n            }\n\n            if (gameObjects.Length > 1 && !HasSameParent(gameObjects))\n                throw new ArgumentException(nameof(gameObjects), \"ApplyAddedGameObjects requires that GameObjects share the same parent.\");\n\n            GameObject gameObject = gameObjects[0];\n            Transform instanceParent = gameObject.transform.parent;\n            if (instanceParent == null)\n                return;\n\n            GameObject prefabSourceGameObjectParent = GetCorrespondingObjectFromSourceAtPath(instanceParent.gameObject, assetPath);\n            if (prefabSourceGameObjectParent == null)\n                return;\n\n            var instanceRoot = GetOutermostPrefabInstanceRoot(instanceParent);\n            if (instanceRoot == null)\n                return;\n\n            var sourceRoot = prefabSourceGameObjectParent.transform.root.gameObject;\n            byte[] originalFileContent = null;\n\n            PrefabUtility.Internal_CallPrefabInstanceApplying(instanceRoot);","sourceCodeStart":1646,"sourceCodeEnd":1682,"githubUrl":"https://github.com/Unity-Technologies/UnityCsReference/blob/225b0fbdb57cc17d094e8056b71f8314aba56f73/Editor/Mono/Prefabs/PrefabUtility.cs#L1646-L1682","documentation":"When applying multiple GameObjects at once, all of them must share the same parent transform so they can be applied into the same target location in the source prefab. The check HasSameParent enforces this precondition before performing the batched apply.","triggerScenarios":"Passing an array whose elements have different transform parents (different siblings groups, different hierarchy depths).","commonSituations":"Combining a multi-selection spanning multiple parts of the hierarchy into a single ApplyAddedGameObjects call; user-selected additions from unrelated parents.","solutions":["Group GameObjects by parent: foreach (var grp in gos.GroupBy(g => g.transform.parent)) ApplyAddedGameObjects(grp.ToArray(), path, mode);","Ensure all selected additions are siblings before submitting the batch.","Fall back to ApplyAddedGameObject per object when parents differ."],"exampleFix":"// before\nPrefabUtility.ApplyAddedGameObjects(mixedParentGos, path, mode);\n\n// after\nforeach (var grp in mixedParentGos.GroupBy(g => g.transform.parent))\n    PrefabUtility.ApplyAddedGameObjects(grp.ToArray(), path, mode);","handlingStrategy":"validation","validationCode":"foreach (var grp in gameObjects.GroupBy(g => g.transform.parent))\n{\n    var arr = grp.ToArray();\n    if (arr.Length > 0)\n        PrefabUtility.ApplyAddedGameObjects(arr, assetPath, action);\n}","typeGuard":"static bool ShareSameParent(GameObject[] gos) =>\n    gos != null && gos.Length > 0 && gos.All(g => g != null\n        && g.transform.parent == gos[0].transform.parent);","tryCatchPattern":null,"preventionTips":["Group additions by parent before batching.","When selections span multiple parents, fall back to per-object ApplyAddedGameObject.","Validate shared parent at the call site to avoid the exception mid-batch."],"tags":["prefab","unity","hierarchy","batch"],"backgroundTag":null,"analyzedSha":"225b0fbdb57cc17d094e8056b71f8314aba56f73","analyzedAt":"2026-08-13T19:07:19.849Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}