{"record":{"id":"920f17b2638131c3","repo":"Unity-Technologies/UnityCsReference","slug":"applyprefabaddedgameobjects-requires-that-gameobje","errorCode":null,"errorMessage":"ApplyPrefabAddedGameObjects requires that GameObjects share the same parent.","messagePattern":"ApplyPrefabAddedGameObjects requires that GameObjects share the same parent\\.","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"Editor/Mono/GUI/TargetChoiceHandler.cs","lineNumber":136,"sourceCode":"        {\n            ObjectInstanceAndSourceInfo info = (ObjectInstanceAndSourceInfo)userData;\n            PrefabUtility.RevertRemovedComponent((GameObject)info.instanceObject, (Component)info.correspondingObjectInSource, InteractionMode.UserAction);\n            EditorUtility.ForceRebuildInspectors();\n        }\n\n        internal static void ApplyPrefabAddedGameObjects(object userData)\n        {\n            ObjectInstanceAndSourcePathInfo[] infos = (ObjectInstanceAndSourcePathInfo[])userData;\n\n            GameObject[] gameObjects = new GameObject[infos.Length];\n            for (int i = 0; i < infos.Length; i++)\n            {\n                ObjectInstanceAndSourcePathInfo info = infos[i];\n                gameObjects[i] = info.instanceObject as GameObject;\n            }\n\n            if (!PrefabUtility.HasSameParent(gameObjects))\n                throw new ArgumentException(nameof(gameObjects), \"ApplyPrefabAddedGameObjects requires that GameObjects share the same parent.\");\n\n            if (!HasSameAssetPath(infos))\n                throw new ArgumentException(nameof(infos), \"ApplyPrefabAddedGameObjects requires that GameObjects share the same parent asset path.\");\n\n            if (!PrefabUtility.PromptAndCheckoutPrefabIfNeeded(infos[0].assetPath, PrefabUtility.SaveVerb.Apply))\n                return;\n\n            PrefabUtility.ApplyAddedGameObjects(gameObjects, infos[0].assetPath, InteractionMode.UserAction);\n            EditorUtility.ForceRebuildInspectors();\n        }\n\n        internal static bool HasSameAssetPath(ObjectInstanceAndSourcePathInfo[] infos)\n        {\n            if (infos == null || infos.Length == 0)\n                throw new ArgumentException(nameof(infos), \"Array is invalid.\");\n\n            string assetPath = infos[0].assetPath;\n            for (int i = 1; i < infos.Length; i++)","sourceCodeStart":118,"sourceCodeEnd":154,"githubUrl":"https://github.com/Unity-Technologies/UnityCsReference/blob/225b0fbdb57cc17d094e8056b71f8314aba56f73/Editor/Mono/GUI/TargetChoiceHandler.cs#L118-L154","documentation":"Thrown by ApplyPrefabAddedGameObjects when applying added GameObjects back into a prefab asset but the supplied GameObjects do not all share the same parent Transform in the hierarchy. PrefabUtility.HasSameParent enforces that all objects belong under one common parent so the apply operation is unambiguous.","triggerScenarios":"Invoking 'Apply Added GameObjects' on a multi-selection of GameObjects that live under different parent transforms; calling ApplyPrefabAddedGameObjects programmatically with a mixed-parent ObjectInstanceAndSourcePathInfo[] array.","commonSituations":"User multi-selects added objects scattered across different parents in a prefab instance and chooses Apply; a custom batch script groups added objects by asset path but not by hierarchy parent.","solutions":["Group the selection by parent transform before calling ApplyPrefabAddedGameObjects, applying each parent-group separately.","Re-parent the added GameObjects so they share a common parent before invoking apply.","Filter the selection to only objects under the same parent and warn the user about excluded siblings."],"exampleFix":"// before\nApplyPrefabAddedGameObjects(infos); // mixed parents\n\n// after\nforeach (var group in infos.GroupBy(i => ((GameObject)i.instanceObject).transform.parent))\n    ApplyPrefabAddedGameObjects(group.ToArray());","handlingStrategy":"validation","validationCode":"bool SameParent(GameObject[] gos) => gos.All(g => g.transform.parent == gos[0].transform.parent);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Group selected added objects by parent before presenting the Apply action.","Disable the Apply menu item when the selection spans multiple parents.","Unit-test apply flows with mixed-parent selections."],"tags":["unity-editor","prefab","hierarchy","argument"],"backgroundTag":null,"analyzedSha":"225b0fbdb57cc17d094e8056b71f8314aba56f73","analyzedAt":"2026-08-13T19:07:19.849Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}