{"record":{"id":"bb3f65022d3a2eb0","repo":"Unity-Technologies/UnityCsReference","slug":"game-object-is-part-of-a-prefab","errorCode":null,"errorMessage":"Game object is part of a prefab","messagePattern":"Game object is part of a prefab","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"Editor/Mono/Prefabs/PrefabUtility.bindings.cs","lineNumber":295,"sourceCode":"                throw new ArgumentException(\"gameObjects array is empty\");\n\n            if (targetPrefab == null)\n                throw new ArgumentNullException(\"targetPrefab\");\n\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                }","sourceCodeStart":277,"sourceCodeEnd":313,"githubUrl":"https://github.com/Unity-Technologies/UnityCsReference/blob/225b0fbdb57cc17d094e8056b71f8314aba56f73/Editor/Mono/Prefabs/PrefabUtility.bindings.cs#L277-L313","documentation":"Thrown when an element `go` in the gameObjects array is persistent (EditorUtility.IsPersistent(go) is true), meaning it is already part of a prefab asset on disk rather than a live scene object. AddGameObjectsToPrefabAndConnect only operates on scene instances, not assets.","triggerScenarios":"Passing a GameObject that lives in the Project view (a prefab asset) rather than in a scene. Mixing project-prefab objects into the array alongside scene objects.","commonSituations":"User multi-selected objects from both the Hierarchy (scene) and Project (assets). Editor scripts that iterate AssetDatabase.FindAssets results and feed asset GameObjects directly. Dragging prefab assets into a list used for connecting scene objects.","solutions":["Pass only scene-based GameObject instances, not assets from the Project window.","Filter out persistent objects: gos.Where(g => !EditorUtility.IsPersistent(g)).","If you intended to nest an existing prefab, use a different API (e.g. PrefabUtility.InstantiatePrefab into the scene first)."],"exampleFix":"// before\nGameObject[] gos = Selection.objects.OfType<GameObject>().ToArray(); // includes assets\nPrefabUtility.AddGameObjectsToPrefabAndConnect(gos, target);\n\n// after\nGameObject[] gos = Selection.gameObjects\n    .Where(g => g != null && !EditorUtility.IsPersistent(g) && g.scene.IsValid())\n    .ToArray();\nPrefabUtility.AddGameObjectsToPrefabAndConnect(gos, target);","handlingStrategy":"validation","validationCode":"gameObjects = gameObjects\n    .Where(g => g != null && !UnityEditor.EditorUtility.IsPersistent(g))\n    .ToArray();","typeGuard":"static bool IsSceneInstance(GameObject g) => g != null && !UnityEditor.EditorUtility.IsPersistent(g) && g.scene.IsValid();","tryCatchPattern":null,"preventionTips":["Only feed Hierarchy (scene) objects, never Project (asset) objects, to connect APIs.","Filter on EditorUtility.IsPersistent and scene validity before bulk calls.","Separate project and hierarchy selections in your tool's UI."],"tags":["prefab","add-to-prefab","persistent-asset","scene-instance"],"backgroundTag":null,"analyzedSha":"225b0fbdb57cc17d094e8056b71f8314aba56f73","analyzedAt":"2026-08-13T19:07:19.849Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}