Unity-Technologies/UnityCsReference · error · ArgumentNullException

path is null or empty

Error message

path is null or empty

What it means

ValidatePath requires a non-null, non-empty path string for prefab save operations. A null or empty path cannot identify a target file, so ArgumentNullException is thrown at the start of the validation chain (before format checks).

Source

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

            if (IsPartOfImmutablePrefab(asset))
                throw new ArgumentException("Can't save an immutable Prefab");

            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))

View on GitHub (pinned to 225b0fbdb5)

Solutions

  1. Build the path from a guaranteed non-empty name under a known folder, e.g., $"Assets/{name}.prefab".
  2. Null/empty check the path before calling: if (!string.IsNullOrEmpty(path)).
  3. Use AssetDatabase.GenerateUniqueAssetPath to produce a valid path from a base.

Example fix

// before
PrefabUtility.SaveAsPrefabAsset(instance, name);

// after
var path = $"Assets/{name}.prefab";
if (!string.IsNullOrEmpty(path))
    PrefabUtility.SaveAsPrefabAsset(instance, path);
Defensive patterns

Strategy: validation

Validate before calling

if (string.IsNullOrEmpty(path)) return;

Type guard

static bool HasNonEmptyPath(string path) => !string.IsNullOrEmpty(path);

Prevention

When it happens

Trigger: Calling a save API that routes through ValidatePath with path == null or path == ""; path constructed from a null/empty name component.

Common situations: Building a path from a missing prefab name field; user left the name blank in a save dialog; concatenation with a null variable.

Related errors


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