Unity-Technologies/UnityCsReference · error · ArgumentNullException

Value cannot be null. (Parameter 'prefabAsset')

Error message

Value cannot be null. (Parameter 'prefabAsset')

What it means

Thrown by ThrowIfInvalidAssetForReplacePrefabInstance when prefabAsset is null. This guard ensures a valid Prefab asset is provided before attempting to replace a Prefab instance with it.

Source

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

        {
            return InstantiatePrefab_internal(assetComponentOrGameObject, EditorSceneManager.GetTargetSceneForNewGameObjects(), null);
        }

        // Instantiates the given prefab in a given scene
        public static Object InstantiatePrefab(Object assetComponentOrGameObject, Scene destinationScene)
        {
            return InstantiatePrefab_internal(assetComponentOrGameObject, destinationScene, null);
        }

        public static Object InstantiatePrefab(Object assetComponentOrGameObject, Transform parent)
        {
            return InstantiatePrefab_internal(assetComponentOrGameObject, EditorSceneManager.GetTargetSceneForNewGameObjects(), parent);
        }

        internal static void ThrowIfInvalidAssetForReplacePrefabInstance(GameObject prefabAsset, InteractionMode action)
        {
            if (prefabAsset == null)
                throw new ArgumentNullException(nameof(prefabAsset));

            if (!EditorUtility.IsPersistent(prefabAsset))
                throw new ArgumentException("Input Prefab asset is not an asset object. Input asset: " + prefabAsset.name, nameof(prefabAsset));

            var assetPath = AssetDatabase.GetAssetPath(prefabAsset);
            if (assetPath.StartsWith("Library/"))
                throw new InvalidOperationException(string.Format("Cannot replace the Prefab instance since the Prefab Asset is invalid for instance replacement. Prefab Asset path: " + assetPath));

            // Recording undo does not handle missing scripts
            var gameObjectsWithInvalidScript = FindGameObjectsWithInvalidComponent(prefabAsset);
            if (action == InteractionMode.UserAction && gameObjectsWithInvalidScript.Count > 0)
                throw new InvalidOperationException(string.Format($"Cannot replace the Prefab instance with the Prefab Asset '{AssetDatabase.GetAssetPath(prefabAsset)}' because it has a missing script. GameObject '{gameObjectsWithInvalidScript[0].name}' in the Prefab Asset has a missing script."));
        }

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

View on GitHub (pinned to 225b0fbdb5)

Solutions

  1. Null-check prefabAsset before calling the replace API.
  2. Verify the asset path with AssetDatabase.LoadAssetAtPath and check for null before proceeding.
  3. Re-resolve asset references after AssetDatabase.Refresh or domain reload.

Example fix

// before
GameObject asset = AssetDatabase.LoadAssetAtPath<GameObject>(badPath);
ThrowIfInvalidAssetForReplacePrefabInstance(asset, mode);
// after
GameObject asset = AssetDatabase.LoadAssetAtPath<GameObject>(path);
if (asset == null) { Debug.LogError("Asset not found: " + path); return; }
ThrowIfInvalidAssetForReplacePrefabInstance(asset, mode);
Defensive patterns

Strategy: type-guard

Validate before calling

if (prefabAsset == null)
{ Debug.LogError("Prefab asset is null; check the asset path."); return; }
PrefabUtility.ThrowIfInvalidAssetForReplacePrefabInstance(prefabAsset, mode);

Type guard

static bool IsValidReplacementAsset(GameObject asset) =>
    asset != null && EditorUtility.IsPersistent(asset);

Prevention

When it happens

Trigger: Calling ReplacePrefabInstance (internal) with a null prefabAsset argument, typically from a failed AssetDatabase lookup or unassigned reference.

Common situations: AssetDatabase.LoadAssetAtPath returned null due to wrong path or missing asset. Serialized Prefab reference lost after domain reload. Editor script assumes an asset exists that was renamed or deleted.

Related errors


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