Unity-Technologies/UnityCsReference · error · ArgumentNullException

The path is null

Error message

The path is null

What it means

Thrown by PrefabUtility.CreateVariant when the destination `path` argument is null. CreateVariant saves a new Prefab Variant asset to disk, so a null path makes no sense and is rejected before any native call. It is an ArgumentNullException, so it represents a programmer error rather than a runtime/environment condition.

Source

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

        // Helper function to find the prefab root of an object
        [FreeFunction]
        [Obsolete("Use GetOutermostPrefabInstanceRoot if source is a Prefab instance or source.transform.root.gameObject if source is a Prefab Asset object.")]
        extern public static GameObject FindPrefabRoot([NotNull] GameObject source);

        internal static GameObject CreateVariant(GameObject assetRoot, string path)
        {
            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);

View on GitHub (pinned to 225b0fbdb5)

Solutions

  1. Pass a non-null asset path string ending in '.prefab', e.g. "Assets/Prefabs/MyVariant.prefab".
  2. Compute the path from a folder selection or AssetDatabase.GenerateUniqueAssetPath before calling CreateVariant.
  3. If the path is user-supplied, validate it is non-null/whitespace before invoking the API.
  4. Guard with `string.IsNullOrEmpty(path)` and either early-return or prompt the user for a valid location.

Example fix

// before
var variant = PrefabUtility.CreateVariant(root, (string)null);

// after
string path = "Assets/Prefabs/MyVariant.prefab";
if (string.IsNullOrEmpty(path))
    throw new InvalidOperationException("Variant path must be set.");
var variant = PrefabUtility.CreateVariant(root, path);
Defensive patterns

Strategy: validation

Validate before calling

if (string.IsNullOrEmpty(path))
    throw new ArgumentException("Variant path must be provided.", nameof(path));

Prevention

When it happens

Trigger: Calling `PrefabUtility.CreateVariant(rootGameObject, null)` or passing a variable that was never assigned an asset path. Triggered after the three earlier guards pass (assetRoot non-null, is a prefab asset, and is a prefab root), so the only remaining unmet condition is `path == null`.

Common situations: Building the destination path dynamically from user input, a text field, or a serialized field that was left empty in the Inspector. Editor scripts that construct the path via string concatenation where one operand is null. Porting code from SaveAsPrefabAsset that omitted the path argument.

Related errors


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