Unity-Technologies/UnityCsReference · error · ArgumentNullException

Parameter root is null

Error message

Parameter root is null

What it means

Thrown by SaveAsPrefabAssetArgumentCheck when instanceRoot is null. This is the first guard before any path or Prefab-state validation — the method cannot operate on a null GameObject reference.

Source

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

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

            if (IsPartOfNonAssetPrefabInstance(instanceRoot))
            {
                // A PrefabInstance with missing asset can be correctly restored only if CorrespondingObjects info is available
                // CorrespondingObject info is available when a PrefabInstance with missing asset was merged before deleting the asset (kNormalMerge) or when it has a scene backup (kMergedAsMissingWithSceneBackup)
                var mergeStatus = GetMergeStatus(instanceRoot);
                var hasCorrespondingSourceObjectInfo = mergeStatus == MergeStatus.NormalMerge || mergeStatus == MergeStatus.MergedAsMissingWithSceneBackup;
                if (IsPrefabAssetMissing(instanceRoot) && !hasCorrespondingSourceObjectInfo)
                    throw new ArgumentException("Can't save Prefab instance with missing asset and scene backup as a Prefab. You may unpack the instance and save the unpacked GameObjects as a Prefab.");
            }

            var actualInstanceRoot = GetOutermostPrefabInstanceRoot(instanceRoot);
            if (actualInstanceRoot)
            {
                if (actualInstanceRoot != instanceRoot)

View on GitHub (pinned to 225b0fbdb5)

Solutions

  1. Null-check instanceRoot before calling SaveAsPrefabAsset.
  2. Verify the GameObject lookup (Find, transform.GetChild index, selection) returns the expected object.
  3. Ensure the reference is assigned via Inspector or properly initialized in code.

Example fix

// before
GameObject go = GameObject.Find("MyObj");
PrefabUtility.SaveAsPrefabAsset(go, "Assets/x.prefab");
// after
GameObject go = GameObject.Find("MyObj");
if (go != null)
    PrefabUtility.SaveAsPrefabAsset(go, "Assets/x.prefab");
Defensive patterns

Strategy: type-guard

Validate before calling

if (instanceRoot == null)
    throw new ArgumentNullException(nameof(instanceRoot));
PrefabUtility.SaveAsPrefabAsset(instanceRoot, path);

Type guard

static bool IsSaveablePrefabSource(GameObject go) =>
    go != null && go.scene.rootCount >= 0;

Prevention

When it happens

Trigger: Calling SaveAsPrefabAsset or SaveAsPrefabAssetAndConnect with instanceRoot == null, typically because a GameObject lookup (GameObject.Find, transform.GetChild, etc.) returned null before the call.

Common situations: GameObject.Find returned null due to inactive object or wrong name. A serialized reference was not assigned in the Inspector. The object was destroyed between lookup and save call.

Related errors


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