{"record":{"id":"cc222fb552442f8a","repo":"Unity-Technologies/UnityCsReference","slug":"target-prefab-has-to-be-a-prefab-asset","errorCode":null,"errorMessage":"Target Prefab has to be a Prefab Asset","messagePattern":"Target Prefab has to be a Prefab Asset","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"Editor/Mono/Prefabs/PrefabUtility.bindings.cs","lineNumber":283,"sourceCode":"        extern private static GameObject SaveAsPrefabAssetAndConnect_Internal([NotNull] GameObject root, string path, out bool success);\n\n        [StaticAccessor(\"PrefabUtilityBindings\", StaticAccessorType.DoubleColon)]\n        [NativeMethod(ThrowsException = true)]\n        extern private static GameObject SavePrefabAsset_Internal([NotNull] GameObject root, out bool success);\n\n        internal static void AddGameObjectsToPrefabAndConnect(GameObject[] gameObjects, Object targetPrefab)\n        {\n            if (gameObjects == null)\n                throw new ArgumentNullException(\"gameObjects\");\n\n            if (gameObjects.Length == 0)\n                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)","sourceCodeStart":265,"sourceCodeEnd":301,"githubUrl":"https://github.com/Unity-Technologies/UnityCsReference/blob/225b0fbdb57cc17d094e8056b71f8314aba56f73/Editor/Mono/Prefabs/PrefabUtility.bindings.cs#L265-L301","documentation":"Thrown by AddGameObjectsToPrefabAndConnect when `targetPrefab` is not null but is not itself a Prefab Asset (PrefabUtility.IsPartOfPrefabAsset returns false). The target must be an asset stored on disk, not a scene instance or a plain GameObject.","triggerScenarios":"Passing a scene instance of a prefab, a disconnected prefab instance, a regular non-prefab GameObject, or a prefab that is an instance/variant-in-scene rather than the asset root.","commonSituations":"Grabbing the target via Selection.activeGameObject while a scene instance (not the asset) is selected. Using the root of an open Prefab edit stage incorrectly. Passing a PrefabAssetHandle/Component instead of the prefab GameObject asset.","solutions":["Pass the Prefab Asset root from the Project window, e.g. loaded via AssetDatabase.LoadAssetAtPath<GameObject>.","Verify with PrefabUtility.IsPartOfPrefabAsset(targetPrefab) before calling.","If you only have an instance, resolve its source asset via PrefabUtility.GetCorrespondingObjectFromSource / GetPrefabAssetHandle and pass that."],"exampleFix":"// before\nGameObject target = Selection.activeGameObject; // a scene instance\nPrefabUtility.AddGameObjectsToPrefabAndConnect(gos, target); // throws\n\n// after\nGameObject target = Selection.activeGameObject;\nif (!PrefabUtility.IsPartOfPrefabAsset(target))\n    target = PrefabUtility.GetCorrespondingObjectFromSource(target);\nif (!PrefabUtility.IsPartOfPrefabAsset(target))\n    throw new ArgumentException(\"Target is not a prefab asset.\");\nPrefabUtility.AddGameObjectsToPrefabAndConnect(gos, target);","handlingStrategy":"validation","validationCode":"if (targetPrefab == null || !PrefabUtility.IsPartOfPrefabAsset(targetPrefab))\n    throw new ArgumentException(\"Target must be a prefab asset on disk.\", nameof(targetPrefab));","typeGuard":"static bool IsPrefabAsset(GameObject obj) => obj != null && PrefabUtility.IsPartOfPrefabAsset(obj);","tryCatchPattern":null,"preventionTips":["Resolve targets from the Project window or AssetDatabase.LoadAssetAtPath, not from scene instances.","Wrap IsPartOfPrefabAsset in a guard helper and use it at every call site.","If only an instance is available, resolve the source asset via GetCorrespondingObjectFromSource first."],"tags":["prefab","add-to-prefab","type-validation"],"backgroundTag":null,"analyzedSha":"225b0fbdb57cc17d094e8056b71f8314aba56f73","analyzedAt":"2026-08-13T19:07:19.849Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}