{"record":{"id":"babccb0c3b9d4000","repo":"Unity-Technologies/UnityCsReference","slug":"can-t-save-a-prefab-instance","errorCode":null,"errorMessage":"Can't save a Prefab instance","messagePattern":"Can't save a Prefab instance","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"Editor/Mono/Prefabs/PrefabUtility.cs","lineNumber":2029,"sourceCode":"            bool savedSuccesfully;\n            return SavePrefabAsset(asset, out savedSuccesfully);\n        }\n\n        public static GameObject SavePrefabAsset(GameObject asset, out bool savedSuccessfully)\n        {\n            if (asset == null)\n                throw new ArgumentNullException(\"Parameter prefabAssetGameObject is null\");\n\n            // Include model check even though models are also immutable, since we can give a more clear exception message.\n            if (IsPartOfModelPrefab(asset))\n                throw new ArgumentException(\"Can't save a Model Prefab\");\n\n            if (IsPartOfImmutablePrefab(asset))\n                throw new ArgumentException(\"Can't save an immutable Prefab\");\n\n            string path = AssetDatabase.GetAssetPath(asset);\n            if (String.IsNullOrEmpty(path))\n                throw new ArgumentException(\"Can't save a Prefab instance\");\n\n            var root = asset.transform.root.gameObject;\n            if (root != asset)\n                throw new ArgumentException(\"GameObject to save Prefab from must be a Prefab root\");\n\n            return SavePrefabAsset_Internal(root, out savedSuccessfully);\n        }\n\n        internal static void ValidatePath(GameObject instanceRoot, string path)\n        {\n            if (String.IsNullOrEmpty(path))\n                throw new ArgumentNullException(\"path is null or empty\");\n\n            if (!Paths.IsValidAssetPath(path, \".prefab\"))\n                throw new ArgumentException(\"Given path is not valid: '\" + path + \"'\");\n\n            if (Directory.Exists(path))\n                throw new ArgumentException(\"Overwriting a folder with an Asset is not allowed: '\" + path + \"'\");","sourceCodeStart":2011,"sourceCodeEnd":2047,"githubUrl":"https://github.com/Unity-Technologies/UnityCsReference/blob/225b0fbdb57cc17d094e8056b71f8314aba56f73/Editor/Mono/Prefabs/PrefabUtility.cs#L2011-L2047","documentation":"SavePrefabAsset requires an asset that has a valid asset path (a saved prefab on disk). If AssetDatabase.GetAssetPath returns null or empty, the object is not a saved prefab asset — typically it is a live prefab instance in a scene — and saving is rejected with ArgumentException.","triggerScenarios":"Calling SavePrefabAsset on a prefab instance that exists in a scene rather than a prefab asset on disk; passing a runtime/unsaved object.","commonSituations":"Confusing an instance in the scene with the asset on disk; selecting a prefab instance and expecting SavePrefabAsset to persist it.","solutions":["Use PrefabUtility.SaveAsPrefabAsset(instance, path) for instances in scenes.","Resolve the asset on disk (via GetPrefabAssetHandle / asset path) before calling SavePrefabAsset.","Check !string.IsNullOrEmpty(AssetDatabase.GetAssetPath(asset)) before saving."],"exampleFix":"// before\nPrefabUtility.SavePrefabAsset(sceneInstance);\n\n// after\nif (!string.IsNullOrEmpty(AssetDatabase.GetAssetPath(sceneInstance)))\n    PrefabUtility.SavePrefabAsset(sceneInstance);\nelse\n    PrefabUtility.SaveAsPrefabAsset(sceneInstance, \"Assets/Saved.prefab\");","handlingStrategy":"validation","validationCode":"string path = AssetDatabase.GetAssetPath(asset);\nif (string.IsNullOrEmpty(path)) return; // it is an instance, not an asset","typeGuard":"static bool IsSavedPrefabAsset(GameObject asset) =>\n    asset != null && !string.IsNullOrEmpty(AssetDatabase.GetAssetPath(asset));","tryCatchPattern":null,"preventionTips":["Use SaveAsPrefabAsset for scene instances; SavePrefabAsset is only for assets on disk.","Check AssetDatabase.GetAssetPath is non-empty before calling SavePrefabAsset.","Resolve the asset via GetPrefabAssetHandle / asset path to distinguish instance from asset."],"tags":["prefab","unity","asset","path"],"backgroundTag":null,"analyzedSha":"225b0fbdb57cc17d094e8056b71f8314aba56f73","analyzedAt":"2026-08-13T19:07:19.849Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}