{"record":{"id":"ba6e0ee1aac87057","repo":"Unity-Technologies/UnityCsReference","slug":"overwriting-a-folder-with-an-asset-is-not-allowed","errorCode":null,"errorMessage":"Overwriting a folder with an Asset is not allowed: '{path}'","messagePattern":"Overwriting a folder with an Asset is not allowed: '(.+?)'","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"Editor/Mono/Prefabs/PrefabUtility.cs","lineNumber":2047,"sourceCode":"                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 + \"'\");\n\n            string directory = Path.GetDirectoryName(path);\n\n            // We allow relative paths outside the Assets folder so we do not throw if isValidAssetFolder is false\n            bool isRootFolder = false;\n            bool isImmutableFolder = false;\n            bool isValidAssetFolder = AssetDatabase.TryGetAssetFolderInfo(directory, out isRootFolder, out isImmutableFolder);\n\n            if (isValidAssetFolder && isImmutableFolder)\n                throw new ArgumentException(\"Saving Prefab to immutable folder is not allowed: '\" + path + \"'\");\n\n            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);","sourceCodeStart":2029,"sourceCodeEnd":2065,"githubUrl":"https://github.com/Unity-Technologies/UnityCsReference/blob/225b0fbdb57cc17d094e8056b71f8314aba56f73/Editor/Mono/Prefabs/PrefabUtility.cs#L2029-L2065","documentation":"Thrown by PrefabUtility.ValidatePath when the target path for saving a Prefab resolves to an existing directory on disk. Unity prevents writing an asset file over a folder because the filesystem conflict would corrupt the operation. The check uses Directory.Exists(path) after confirming the path is a non-empty, valid .prefab asset path.","triggerScenarios":"Calling SaveAsPrefabAsset, SaveAsPrefabAssetAndConnect, or any internal save path where ValidatePath is invoked, with an assetPath string that matches an existing folder name (e.g. \"Assets/MyFolder\" when MyFolder exists as a directory). Can also occur if the path has no \".prefab\" extension but still passes IsValidAssetPath and coincides with a real directory.","commonSituations":"Developer passes a folder path instead of a full file path (forgets to append \"/MyPrefab.prefab\"). Path constructed by concatenating a directory variable without a filename. Migrating from older Unity APIs that were more lenient about path formatting.","solutions":["Ensure assetPath ends with a filename and .prefab extension, e.g. \"Assets/Prefabs/MyPrefab.prefab\" not \"Assets/Prefabs\".","Check Directory.Exists(path) before calling SaveAsPrefabAsset and append a filename if true.","Use Path.Combine with an explicit filename component rather than passing a bare folder path."],"exampleFix":"// before\nPrefabUtility.SaveAsPrefabAsset(go, \"Assets/Prefabs\");\n// after\nPrefabUtility.SaveAsPrefabAsset(go, \"Assets/Prefabs/MyPrefab.prefab\");","handlingStrategy":"validation","validationCode":"void SavePrefabSafe(GameObject go, string path)\n{\n    if (Directory.Exists(path))\n        throw new ArgumentException(\"Path is a folder, append a filename: \" + path);\n    if (!path.EndsWith(\".prefab\"))\n        path = Path.Combine(path, go.name + \".prefab\");\n    PrefabUtility.SaveAsPrefabAsset(go, path);\n}","typeGuard":"static bool IsValidPrefabFilePath(string path) =>\n    !string.IsNullOrEmpty(path) &&\n    path.EndsWith(\".prefab\") &&\n    !Directory.Exists(path) &&\n    Paths.IsValidAssetPath(path, \".prefab\");","tryCatchPattern":null,"preventionTips":["Always build Prefab paths with an explicit filename + .prefab extension.","Run a Directory.Exists check on the full path before saving.","Centralize Prefab save logic in a helper that validates the path shape."],"tags":["unity","prefab","path-validation","filesystem"],"backgroundTag":null,"analyzedSha":"225b0fbdb57cc17d094e8056b71f8314aba56f73","analyzedAt":"2026-08-13T19:07:19.849Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}