{"record":{"id":"961bfbfc56fbc7cc","repo":"Unity-Technologies/UnityCsReference","slug":"gameobject-is-not-directly-parented-under-a-targ","errorCode":null,"errorMessage":"GameObject is not (directly) parented under a target Prefab instance.","messagePattern":"GameObject is not \\(directly\\) parented under a target Prefab instance\\.","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"Editor/Mono/Prefabs/PrefabUtility.bindings.cs","lineNumber":299,"sourceCode":"\n            if (!PrefabUtility.IsPartOfPrefabAsset(targetPrefab))\n                throw new ArgumentException(\"Target Prefab has to be a Prefab Asset\");\n\n            Object targetPrefabInstance = null;\n\n            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);","sourceCodeStart":281,"sourceCodeEnd":317,"githubUrl":"https://github.com/Unity-Technologies/UnityCsReference/blob/225b0fbdb57cc17d094e8056b71f8314aba56f73/Editor/Mono/Prefabs/PrefabUtility.bindings.cs#L281-L317","documentation":"Thrown when GetParentPrefabInstance(go) returns null, meaning the GameObject is not directly parented under any Prefab instance in the scene hierarchy. Each go must live inside (be a child of) a prefab instance to be connected into a target prefab.","triggerScenarios":"Passing a top-level scene object that is not inside any prefab instance, or an object whose chain to a prefab root is broken/disconnected. A loose GameObject placed directly in the scene root.","commonSituations":"Objects created via new GameObject() in the scene then passed in. Objects reparented out of a prefab instance. Prefab instances that were unpacked, severing the instance link.","solutions":["Parent the GameObject under an existing Prefab instance in the scene before calling.","Instantiate via PrefabUtility.InstantiatePrefab so the object is part of an instance.","Filter to objects whose ancestor chain includes a prefab instance root."],"exampleFix":"// before\nvar go = new GameObject(\"Loose\");\nPrefabUtility.AddGameObjectsToPrefabAndConnect(new[]{ go }, target); // not under instance\n\n// after\nvar instance = (GameObject)PrefabUtility.InstantiatePrefab(target);\ngo.transform.SetParent(instance.transform);\nPrefabUtility.AddGameObjectsToPrefabAndConnect(new[]{ go }, target);","handlingStrategy":"validation","validationCode":"gameObjects = gameObjects\n    .Where(g => PrefabUtility.GetOutermostPrefabInstanceRoot(g) != null)\n    .ToArray();","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Ensure objects are parented under a prefab instance before connecting.","Instantiate via PrefabUtility.InstantiatePrefab rather than new GameObject to keep instance links.","Do not UnpackPrefabInstance on objects you intend to connect later."],"tags":["prefab","add-to-prefab","hierarchy","parent-instance"],"backgroundTag":null,"analyzedSha":"225b0fbdb57cc17d094e8056b71f8314aba56f73","analyzedAt":"2026-08-13T19:07:19.849Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}