{"record":{"id":"1fa5a5e9691fd004","repo":"Unity-Technologies/UnityCsReference","slug":"can-t-save-persistent-objects-and-connect-them-to","errorCode":null,"errorMessage":"Can't save persistent Objects and connect them to the saved Prefab","messagePattern":"Can't save persistent Objects and connect them to the saved Prefab","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"Editor/Mono/Prefabs/PrefabUtility.cs","lineNumber":2077,"sourceCode":"            if (directory.Length > 0 && !Directory.Exists(directory))\n                throw new ArgumentException(\"Given path does not exist: '\" + path + \"'\");\n\n            if (isValidAssetFolder)\n            {\n                string projectRelativePath = Path.IsPathRooted(path) ? FileUtil.GetProjectRelativePath(path) : path;\n                string prefabGUID = AssetDatabase.AssetPathToGUID(projectRelativePath);\n                if (!VerifyNestingFromScript(new GameObject[] { instanceRoot }, prefabGUID, PrefabUtility.GetPrefabInstanceHandle(instanceRoot)))\n                    throw new ArgumentException(\"Cyclic nesting detected\");\n            }\n        }\n\n        private static void SaveAsPrefabAssetArgumentCheck(GameObject instanceRoot, string path, bool connectToInstance)\n        {\n            if (instanceRoot == null)\n                throw new ArgumentNullException(\"Parameter root is null\");\n\n            if (EditorUtility.IsPersistent(instanceRoot) && connectToInstance)\n                throw new ArgumentException(\"Can't save persistent Objects and connect them to the saved Prefab\");\n\n            if (IsPartOfNonAssetPrefabInstance(instanceRoot))\n            {\n                // A PrefabInstance with missing asset can be correctly restored only if CorrespondingObjects info is available\n                // CorrespondingObject info is available when a PrefabInstance with missing asset was merged before deleting the asset (kNormalMerge) or when it has a scene backup (kMergedAsMissingWithSceneBackup)\n                var mergeStatus = GetMergeStatus(instanceRoot);\n                var hasCorrespondingSourceObjectInfo = mergeStatus == MergeStatus.NormalMerge || mergeStatus == MergeStatus.MergedAsMissingWithSceneBackup;\n                if (IsPrefabAssetMissing(instanceRoot) && !hasCorrespondingSourceObjectInfo)\n                    throw new ArgumentException(\"Can't save Prefab instance with missing asset and scene backup as a Prefab. You may unpack the instance and save the unpacked GameObjects as a Prefab.\");\n            }\n\n            var actualInstanceRoot = GetOutermostPrefabInstanceRoot(instanceRoot);\n            if (actualInstanceRoot)\n            {\n                if (actualInstanceRoot != instanceRoot)\n                    throw new ArgumentException(\"Can't save part of a Prefab instance as a Prefab\");\n            }\n","sourceCodeStart":2059,"sourceCodeEnd":2095,"githubUrl":"https://github.com/Unity-Technologies/UnityCsReference/blob/225b0fbdb57cc17d094e8056b71f8314aba56f73/Editor/Mono/Prefabs/PrefabUtility.cs#L2059-L2095","documentation":"Thrown by SaveAsPrefabAssetArgumentCheck when the instanceRoot is a persistent object (EditorUtility.IsPersistent returns true) and connectToInstance is true. Persistent objects live in the AssetDatabase, not in a scene, so connecting them to a scene instance is meaningless and disallowed.","triggerScenarios":"Calling SaveAsPrefabAssetAndConnect (which passes connectToInstance=true) with a GameObject that is already an asset (e.g. a Prefab asset loaded via AssetDatabase.LoadAssetAtPath), rather than a scene-level instance.","commonSituations":"Developer loads a Prefab asset and tries to \"save and connect\" it, not realizing connect is a scene-instance operation. Confusion between SaveAsPrefabAsset (no connect) and SaveAsPrefabAssetAndConnect (connect).","solutions":["Use SaveAsPrefabAsset (without connect) when operating on an existing asset, not SaveAsPrefabAssetAndConnect.","If you need a connected scene instance, first instantiate the asset into a scene then save/connect from there.","Check EditorUtility.IsPersistent(instanceRoot) before calling the connect variant."],"exampleFix":"// before\nGameObject asset = AssetDatabase.LoadAssetAtPath<GameObject>(\"Assets/x.prefab\");\nPrefabUtility.SaveAsPrefabAssetAndConnect(asset, \"Assets/y.prefab\", InteractionMode.AutomatedAction);\n// after\nGameObject asset = AssetDatabase.LoadAssetAtPath<GameObject>(\"Assets/x.prefab\");\nPrefabUtility.SaveAsPrefabAsset(asset, \"Assets/y.prefab\");","handlingStrategy":"validation","validationCode":"void SavePrefabAuto(GameObject go, string path, bool connect)\n{\n    if (connect && EditorUtility.IsPersistent(go))\n    {\n        Debug.LogWarning(\"Object is persistent; using SaveAsPrefabAsset (no connect).\");\n        PrefabUtility.SaveAsPrefabAsset(go, path);\n    }\n    else if (connect)\n        PrefabUtility.SaveAsPrefabAssetAndConnect(go, path, InteractionMode.AutomatedAction);\n    else\n        PrefabUtility.SaveAsPrefabAsset(go, path);\n}","typeGuard":"static bool CanConnectToScene(GameObject go) =>\n    go != null && !EditorUtility.IsPersistent(go);","tryCatchPattern":null,"preventionTips":["Reserve SaveAsPrefabAssetAndConnect for scene-level instances only.","Check EditorUtility.IsPersistent before choosing the connect variant.","Document clearly which API each helper uses to avoid misuse."],"tags":["unity","prefab","persistent-object","argument-check"],"backgroundTag":null,"analyzedSha":"225b0fbdb57cc17d094e8056b71f8314aba56f73","analyzedAt":"2026-08-13T19:07:19.849Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}