Unity-Technologies/UnityCsReference · error · ArgumentException
Given path is not valid: '{path}'
Error message
Given path is not valid: '{path}' What it means
ValidatePath throws ArgumentException when Paths.IsValidAssetPath(path, ".prefab") fails — the path is not a syntactically valid prefab asset path. This covers wrong/missing extension, invalid characters, or a path that does not conform to Unity's asset path rules.
Source
Thrown at Editor/Mono/Prefabs/PrefabUtility.cs:2044
string path = AssetDatabase.GetAssetPath(asset);
if (String.IsNullOrEmpty(path))
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)View on GitHub (pinned to 225b0fbdb5)
Solutions
- Ensure the path is relative to the project folder and ends with .prefab.
- Normalize separators to forward slashes and use AssetDatabase.GenerateUniqueAssetPath for a valid base.
- Verify with Paths.IsValidAssetPath(path, ".prefab") before calling when accessible, or check extension and characters manually.
Example fix
// before PrefabUtility.SaveAsPrefabAsset(instance, @"C:\\prefabs\\MyPrefab"); // after string path = "Assets/Prefabs/MyPrefab.prefab"; path = AssetDatabase.GenerateUniqueAssetPath(path); PrefabUtility.SaveAsPrefabAsset(instance, path);
Defensive patterns
Strategy: validation
Validate before calling
if (string.IsNullOrEmpty(path) || !path.EndsWith(".prefab")) return;
path = path.Replace('\\', '/'); Type guard
static bool IsValidPrefabPath(string path) =>
!string.IsNullOrEmpty(path) && path.EndsWith(".prefab")
&& path.IndexOfAny(Path.GetInvalidPathChars()) < 0; Prevention
- Use forward slashes and a project-relative path ending in .prefab.
- Normalize separators before constructing asset paths.
- Prefer AssetDatabase.GenerateUniqueAssetPath to guarantee a valid, collision-free path.
When it happens
Trigger: Passing a path without the .prefab extension, with backslashes on a project that expects forward slashes, with illegal characters, or an absolute path instead of a project-relative one.
Common situations: Mixing System.IO.Path separators with Unity asset paths; forgetting the extension; passing an absolute OS path; copy-pasting a path with stray characters.
Related errors
- path is null or empty
- Incorrect file extension: {prefabAssetPath}. Must be '.prefa
- guid
- prefabInstance
- Provided GameObject is not a Prefab instance
AI-assisted analysis of Unity-Technologies/UnityCsReference@225b0fbdb5 (2026-08-13).
Data as JSON: /api/errors/93d67ee7ea0042b6.
Report an issue: GitHub.