Unity-Technologies/UnityCsReference · error · ArgumentException

Given path is not valid: '{path}'

Error message

Given path is not valid: '{path}'

What it means

Thrown by CreateVariant when `Paths.IsValidAssetPath(path, ".prefab")` returns false. This validates that the path lives under the project's Assets folder, is a valid asset path string, and ends with the required '.prefab' extension. It fires only after the null and source-collision checks pass.

Source

Thrown at Editor/Mono/Prefabs/PrefabUtility.bindings.cs:244

        {
            if (assetRoot == null)
                throw new ArgumentNullException("The inputObject is null");

            if (!IsPartOfPrefabAsset(assetRoot))
                throw new ArgumentException("Given input object is not a prefab asset");

            if (assetRoot.transform.root.gameObject != assetRoot)
                throw new ArgumentException("Object to create variant from has to be a Prefab root");

            if (path == null)
                throw new ArgumentNullException("The path is null");

            var assetRootObjectPath = AssetDatabase.GetAssetPath(assetRoot);
            if (Paths.AreEqual(path, assetRootObjectPath, true))
                throw new ArgumentException("Creating a variant of an object into the source file of the input object is not allowed");

            if (!Paths.IsValidAssetPath(path, ".prefab"))
                throw new ArgumentException("Given path is not valid: '" + path + "'");

            return CreateVariant_Internal(assetRoot, path);
        }

        [NativeMethod("CreateVariant", IsFreeFunction = true)]
        extern private static GameObject CreateVariant_Internal([NotNull] GameObject original, string path);

        [StaticAccessor("PrefabUtilityBindings", StaticAccessorType.DoubleColon)]
        [NativeMethod(ThrowsException = true)]
        extern private static GameObject SavePrefab_Internal([NotNull] GameObject root, string path, bool connectToInstance, out bool success);

        [StaticAccessor("PrefabUtilityBindings", StaticAccessorType.DoubleColon)]
        extern private static GameObject ApplyPrefabInstance_Internal([NotNull] GameObject root);

        [StaticAccessor("PrefabUtilityBindings", StaticAccessorType.DoubleColon)]
        [NativeMethod(ThrowsException = true)]
        extern private static GameObject SaveAsPrefabAsset_Internal([NotNull] GameObject root, string path, out bool success);

View on GitHub (pinned to 225b0fbdb5)

Solutions

  1. Use a Unity-relative path rooted in Assets with a .prefab extension: "Assets/Prefabs/Name.prefab".
  2. Ensure the parent directory exists under Assets (use AssetDatabase to create folders if needed).
  3. Build the path with AssetDatabase.GenerateUniqueAssetPath to normalize it.
  4. Strip drive letters and backslashes; convert to forward slashes and an Assets/ prefix.

Example fix

// before
var v = PrefabUtility.CreateVariant(root, @"C:\Proj\Variant.prefab"); // invalid

// after
string rel = "Assets/Prefabs/" + root.name + "Variant.prefab";
rel = AssetDatabase.GenerateUniqueAssetPath(rel);
var v = PrefabUtility.CreateVariant(root, rel);
Defensive patterns

Strategy: validation

Validate before calling

if (!path.StartsWith("Assets/") || !path.EndsWith(".prefab", System.StringComparison.OrdinalIgnoreCase))
    path = AssetDatabase.GenerateUniqueAssetPath("Assets/Prefabs/" + assetRoot.name + "Variant.prefab");

Prevention

When it happens

Trigger: Passing an absolute filesystem path (e.g. "C:/.../Foo.prefab"), a path outside the Assets folder, a path with no '.prefab' extension, a path with a different extension, or a path containing illegal characters/empty segments.

Common situations: Using System.IO absolute paths instead of Unity-relative 'Assets/...' paths. Forgetting the '.prefab' extension. Typing a folder that does not exist under Assets. Copying a path from Explorer that includes a drive letter.

Related errors


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