Unity-Technologies/UnityCsReference · error · ArgumentException

Given path does not exist: '{path}'

Error message

Given path does not exist: '{path}'

What it means

Thrown by ValidatePath when the parent directory of the target Prefab path does not exist on disk. Unity requires the destination folder to exist before writing the .prefab file; it does not auto-create intermediate directories during Prefab save operations.

Source

Thrown at Editor/Mono/Prefabs/PrefabUtility.cs:2060

            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);
                if (!VerifyNestingFromScript(new GameObject[] { instanceRoot }, prefabGUID, PrefabUtility.GetPrefabInstanceHandle(instanceRoot)))
                    throw new ArgumentException("Cyclic nesting detected");
            }
        }

        private static void SaveAsPrefabAssetArgumentCheck(GameObject instanceRoot, string path, bool connectToInstance)
        {
            if (instanceRoot == null)
                throw new ArgumentNullException("Parameter root is null");

            if (EditorUtility.IsPersistent(instanceRoot) && connectToInstance)
                throw new ArgumentException("Can't save persistent Objects and connect them to the saved Prefab");

View on GitHub (pinned to 225b0fbdb5)

Solutions

  1. Create the directory before saving: if (!Directory.Exists(dir)) Directory.CreateDirectory(dir);
  2. Use AssetDatabase.CreateFolder("Assets", "NewFolder") for folders inside the Assets hierarchy.
  3. Verify the path is relative to the project root and the parent exists before constructing the full path.

Example fix

// before
PrefabUtility.SaveAsPrefabAsset(go, "Assets/NewFolder/My.prefab");
// after
string dir = "Assets/NewFolder";
if (!AssetDatabase.IsValidFolder(dir))
    AssetDatabase.CreateFolder("Assets", "NewFolder");
PrefabUtility.SaveAsPrefabAsset(go, "Assets/NewFolder/My.prefab");
Defensive patterns

Strategy: validation

Validate before calling

void EnsureDirectoryExists(string path)
{
    string dir = Path.GetDirectoryName(path);
    if (!string.IsNullOrEmpty(dir) && !Directory.Exists(dir))
    {
        string parent = Path.GetDirectoryName(dir);
        string folder = Path.GetFileName(dir);
        if (AssetDatabase.IsValidFolder(parent))
            AssetDatabase.CreateFolder(parent, folder);
        else
            Directory.CreateDirectory(dir);
    }
}

Type guard

static bool DirectoryForPathExists(string path) =>
    string.IsNullOrEmpty(Path.GetDirectoryName(path)) ||
    Directory.Exists(Path.GetDirectoryName(path));

Prevention

When it happens

Trigger: Calling a SaveAsPrefab API with a path whose directory portion is valid (resolves inside Assets) but has not been created yet. The check is directory.Length > 0 && !Directory.Exists(directory).

Common situations: Developer assumes Unity creates missing folders automatically. Path is dynamically generated with a subfolder that hasn't been created. Moving from a machine where the folder existed to a fresh checkout.

Related errors


AI-assisted analysis of Unity-Technologies/UnityCsReference@225b0fbdb5 (2026-08-13). Data as JSON: /api/errors/df83a72b2007142e. Report an issue: GitHub.