{"record":{"id":"3c6137304cf2ffa6","repo":"Unity-Technologies/UnityCsReference","slug":"gameobject-is-not-parented-under-a-target-prefab-i","errorCode":null,"errorMessage":"GameObject is not parented under a target Prefab instance.","messagePattern":"GameObject is not parented under a target Prefab instance\\.","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"Editor/Mono/Prefabs/PrefabUtility.bindings.cs","lineNumber":305,"sourceCode":"            var targetPrefabObject = PrefabUtility.GetPrefabAssetHandle(targetPrefab);\n\n            foreach (GameObject go in gameObjects)\n            {\n                if (go == null)\n                    throw new ArgumentException(\"GameObject in input 'gameObjects' array is null\");\n\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","sourceCodeStart":287,"sourceCodeEnd":323,"githubUrl":"https://github.com/Unity-Technologies/UnityCsReference/blob/225b0fbdb57cc17d094e8056b71f8314aba56f73/Editor/Mono/Prefabs/PrefabUtility.bindings.cs#L287-L323","documentation":"Thrown on the FIRST processed GameObject: after capturing targetPrefabInstance from its parent, Unity checks IsPrefabInstanceObjectOf(go.transform.parent, targetPrefabObject). If the parent instance is not derived from the target prefab asset, the object is considered under the wrong instance.","triggerScenarios":"The GameObject is parented under a different prefab instance (a different prefab asset) than `targetPrefab`. Combining objects from prefab A's instance while trying to connect them into prefab B.","commonSituations":"Mismatched selection: user selected objects under one prefab but specified a different target prefab. Script wiring the wrong target to a selection sourced from another prefab.","solutions":["Ensure the selected GameObject's parent prefab instance is an instance of `targetPrefab`.","Resolve the target prefab from the first selected object's own instance to guarantee a match.","Validate IsPrefabInstanceObjectOf(go.transform.parent, targetHandle) before calling."],"exampleFix":"// before\nGameObject[] gos = FindObjectsUnderPrefabA();\nPrefabUtility.AddGameObjectsToPrefabAndConnect(gos, prefabB); // mismatch\n\n// after\nGameObject[] gos = FindObjectsUnderPrefabA();\nvar targetHandle = PrefabUtility.GetPrefabAssetHandle(prefabA);\n// confirm parents match targetHandle, then:\nPrefabUtility.AddGameObjectsToPrefabAndConnect(gos, prefabA);","handlingStrategy":"validation","validationCode":"var targetHandle = PrefabUtility.GetPrefabAssetHandle(targetPrefab);\nbool ok = gameObjects.All(g =>\n    IsPrefabInstanceObjectOf(g.transform.parent, targetHandle));\nif (!ok) throw new ArgumentException(\"Objects must be under an instance of the target prefab.\");","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Derive the target prefab from the selected objects' own instance to guarantee a match.","Validate IsPrefabInstanceObjectOf on the first object's parent before the batch call.","Keep target and selection sourced from the same prefab asset."],"tags":["prefab","add-to-prefab","instance-mismatch","hierarchy"],"backgroundTag":null,"analyzedSha":"225b0fbdb57cc17d094e8056b71f8314aba56f73","analyzedAt":"2026-08-13T19:07:19.849Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}