Unity-Technologies/UnityCsReference · error · ArgumentNullException

plainGameObject

Error message

plainGameObject

What it means

Thrown by ThrowIfInvalidArgumentsForConvertToPrefabInstance when plainGameObject is null. This is the standard ArgumentNullException guard at the top of the validation method for ConvertToPrefabInstance. The argument name 'plainGameObject' is used via nameof.

Source

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

                Undo.FlushTrackedObjects();
                Undo.SetCurrentGroupName(undoActionName);
                Undo.RegisterFullObjectHierarchyUndo(prefabInstanceRoot, undoActionName);
            }

            bool success = ReplacePrefabAssetOfPrefabInstance_Internal(prefabInstanceRoot, prefabAssetRoot, settings);
            if (!success)
            {
                Debug.LogError(string.Format("Replace Prefab Instance failed for instance '{0}' using asset '{1}' at '{2}'", prefabInstanceRoot.name, prefabAssetRoot.name, AssetDatabase.GetAssetPath(prefabAssetRoot)), prefabInstanceRoot);
            }

            if (mode == InteractionMode.UserAction)
                Undo.FlushTrackedObjects();
        }

        internal static void ThrowIfInvalidArgumentsForConvertToPrefabInstance(GameObject plainGameObject, GameObject prefabAssetRoot, bool checkValidAsset, InteractionMode mode)
        {
            if (plainGameObject == null)
                throw new ArgumentNullException(nameof(plainGameObject));

            if (prefabAssetRoot == null)
                throw new ArgumentNullException(nameof(prefabAssetRoot));

            if (IsPartOfNonAssetPrefabInstance(plainGameObject))
                throw new InvalidOperationException(string.Format("Input '{0}' is not a plain GameObject, it is already a Prefab instance. Use ReplacePrefabAssetOfPrefabInstance() instead.", plainGameObject.name));

            if (EditorUtility.IsPersistent(plainGameObject))
                throw new ArgumentException("Input is from a Prefab asset, this is not supported. Input GameObject: " + plainGameObject.name, nameof(plainGameObject));

            if (checkValidAsset)
                ThrowIfInvalidAssetForReplacePrefabInstance(prefabAssetRoot, mode);

            if (plainGameObject.transform.GetType() != prefabAssetRoot.transform.GetType())
                throw new InvalidOperationException(string.Format("Cannot convert the GameObject '{0}' with root transform of type {1} with a Prefab asset with root transform of type {2}. Transform types must match.", plainGameObject.name, plainGameObject.transform.GetType().Name, prefabAssetRoot.transform.GetType().Name));

            // Prefab Mode and EditPrefabContents scope handling
            if (PrefabStageUtility.IsGameObjectThePrefabRootInAnyPrefabStage(plainGameObject) || (EditorSceneManager.IsPreviewSceneObject(plainGameObject) && plainGameObject.transform.parent == null))

View on GitHub (pinned to 225b0fbdb5)

Solutions

  1. Null-check plainGameObject before calling ConvertToPrefabInstance.
  2. Validate that Selection.activeGameObject is non-null in editor menu actions.
  3. Use the null-conditional operator or early-return guards.

Example fix

// before
PrefabUtility.ConvertToPrefabInstance(target, prefabAsset, settings, mode);
// target could be null

// after
if (target != null)
    PrefabUtility.ConvertToPrefabInstance(target, prefabAsset, settings, mode);
Defensive patterns

Strategy: validation

Validate before calling

if (plainGameObject == null)
{
    Debug.LogError("plainGameObject is null.");
    return;
}
PrefabUtility.ConvertToPrefabInstance(plainGameObject, prefabAssetRoot, settings, mode);

Prevention

When it happens

Trigger: Calling PrefabUtility.ConvertToPrefabInstance(null, prefabAsset, settings, mode) — e.g. passing a reference that was never assigned or a destroyed object.

Common situations: Object reference lost after DestroyImmediate in the same frame. Deserialized data pointing to a destroyed asset. Selection.activeGameObject returning null when nothing is selected.

Related errors


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