Unity-Technologies/UnityCsReference · error · ArgumentException
Overwriting a folder with an Asset is not allowed: '{path}'
Error message
Overwriting a folder with an Asset is not allowed: '{path}' What it means
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.
Source
Thrown at Editor/Mono/Prefabs/PrefabUtility.cs:2047
throw new ArgumentException("Can't save a Prefab instance");
var root = asset.transform.root.gameObject;
if (root != asset)
throw new ArgumentException("GameObject to save Prefab from must be a Prefab root");
return SavePrefabAsset_Internal(root, out savedSuccessfully);
}
internal static void ValidatePath(GameObject instanceRoot, string path)
{
if (String.IsNullOrEmpty(path))
throw new ArgumentNullException("path is null or empty");
if (!Paths.IsValidAssetPath(path, ".prefab"))
throw new ArgumentException("Given path is not valid: '" + path + "'");
if (Directory.Exists(path))
throw new ArgumentException("Overwriting a folder with an Asset is not allowed: '" + path + "'");
string directory = Path.GetDirectoryName(path);
// We allow relative paths outside the Assets folder so we do not throw if isValidAssetFolder is false
bool isRootFolder = false;
bool isImmutableFolder = false;
bool isValidAssetFolder = AssetDatabase.TryGetAssetFolderInfo(directory, out isRootFolder, out isImmutableFolder);
if (isValidAssetFolder && isImmutableFolder)
throw new ArgumentException("Saving Prefab to immutable folder is not allowed: '" + path + "'");
if (directory.Length > 0 && !Directory.Exists(directory))
throw new ArgumentException("Given path does not exist: '" + path + "'");
if (isValidAssetFolder)
{
string projectRelativePath = Path.IsPathRooted(path) ? FileUtil.GetProjectRelativePath(path) : path;
string prefabGUID = AssetDatabase.AssetPathToGUID(projectRelativePath);View on GitHub (pinned to 225b0fbdb5)
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.
Example fix
// before PrefabUtility.SaveAsPrefabAsset(go, "Assets/Prefabs"); // after PrefabUtility.SaveAsPrefabAsset(go, "Assets/Prefabs/MyPrefab.prefab");
Defensive patterns
Strategy: validation
Validate before calling
void SavePrefabSafe(GameObject go, string path)
{
if (Directory.Exists(path))
throw new ArgumentException("Path is a folder, append a filename: " + path);
if (!path.EndsWith(".prefab"))
path = Path.Combine(path, go.name + ".prefab");
PrefabUtility.SaveAsPrefabAsset(go, path);
} Type guard
static bool IsValidPrefabFilePath(string path) =>
!string.IsNullOrEmpty(path) &&
path.EndsWith(".prefab") &&
!Directory.Exists(path) &&
Paths.IsValidAssetPath(path, ".prefab"); Prevention
- 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.
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- Saving Prefab to immutable folder is not allowed: '{path}'
- Given path does not exist: '{path}'
- Can't connect a Prefab in the StreamingAssets folder to Game
- Scene path "{0}" contains invalid directory separators.
- Invalid build path: '{buildPlayerOptions.locationPathName}'.
AI-assisted analysis of Unity-Technologies/UnityCsReference@225b0fbdb5 (2026-08-13).
Data as JSON: /api/errors/ba6e0ee1aac87057.
Report an issue: GitHub.