Unity-Technologies/UnityCsReference · error · ArgumentNullException

Value cannot be null. (Parameter 'instance')

Error message

Value cannot be null. (Parameter 'instance')

What it means

Thrown by ApplyPrefabInstance when the instance argument is null. This is the entry guard: applying overrides to a Prefab requires a non-null GameObject reference.

Source

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

            var assetRoot = SaveAsPrefabAssetAndConnect_Internal(instanceRoot, assetPath, out success);

            if (!success)
            {
                return null;
            }

            if (action == InteractionMode.UserAction)
            {
                Undo.RecordCreatedObject(GetPrefabInstanceHandle(instanceRoot), actionName);
            }

            return assetRoot;
        }

        internal static void ApplyPrefabInstance(GameObject instance)
        {
            if (instance == null)
                throw new ArgumentNullException(nameof(instance));

            // Include model check even though models are also immutable, since we can give a more clear exception message.
            if (IsPartOfModelPrefab(instance))
                throw new ArgumentException("Can't apply to a Model Prefab");

            if (IsPartOfImmutablePrefab(instance))
                throw new ArgumentException("Can't apply to an immutable Prefab");

            if (!IsPartOfNonAssetPrefabInstance(instance))
                throw new ArgumentException("Provided GameObject is not a Prefab instance");

            var root = GetOutermostPrefabInstanceRoot(instance);
            if (root != instance)
                throw new ArgumentException("GameObject to save Prefab from must be a Prefab root");

            var assetObject = GetCorrespondingObjectFromSource(instance);
            string path = AssetDatabase.GetAssetPath(assetObject);

View on GitHub (pinned to 225b0fbdb5)

Solutions

  1. Null-check instance before calling ApplyPrefabInstance.
  2. Verify the selection or lookup that produced the reference is valid in the current editor context.
  3. Guard editor menu item handlers with [MenuItem(...)] validation or ValidateFunction.

Example fix

// before
PrefabUtility.ApplyPrefabInstance(Selection.activeGameObject);
// after
GameObject sel = Selection.activeGameObject;
if (sel != null)
    PrefabUtility.ApplyPrefabInstance(sel);
Defensive patterns

Strategy: type-guard

Validate before calling

if (instance == null)
{ Debug.LogError("Cannot apply: instance is null."); return; }
PrefabUtility.ApplyPrefabInstance(instance);

Type guard

static bool IsApplyableInstance(GameObject go) =>
    go != null && PrefabUtility.IsPartOfNonAssetPrefabInstance(go);

Prevention

When it happens

Trigger: Calling PrefabUtility.ApplyPrefabInstance(null) or passing a null GameObject from a failed lookup.

Common situations: Selection.activeGameObject returns null (nothing selected). A GameObject reference became null after Undo or scene load. Editor script runs in a context where no instance is selected.

Related errors


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