Unity-Technologies/UnityCsReference · error · ArgumentNullException

Prefab source may not be null.

Error message

Prefab source may not be null.

What it means

Thrown by ApplyRemovedComponent(GameObject instanceGameObject, Component assetComponent, InteractionMode) when assetComponent is null — ArgumentNullException(nameof(assetComponent)). The asset component is the only way to identify which component was removed from the instance (matched by FileID), so a null source makes the operation impossible.

Source

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

                    filteredRemovedComponents.Add(assetComponent);
                }
            }

            if (filteredRemovedComponents.Count != removedComponents.Length)
                SetRemovedComponents(prefabInstanceObject, filteredRemovedComponents.ToArray());
        }

        // We can't use the same pattern of identifying the prefab asset via assetPath only,
        // since when the component is removed in the instance, the only way to identify which component it is,
        // is via the corresponding component on the asset. We find it by matching FileIds. Additionally supplying an assetPath would be redundant.
        public static void ApplyRemovedComponent(GameObject instanceGameObject, Component assetComponent, InteractionMode action)
        {
            DateTime startTime = DateTime.UtcNow;

            ThrowExceptionIfNotValidPrefabInstanceObject(instanceGameObject, true);

            if (assetComponent == null)
                throw new ArgumentNullException(nameof(assetComponent), "Prefab source may not be null.");

            var actionName = "Apply Prefab removed component";

            if (action == InteractionMode.UserAction)
            {
                string dependentComponents = string.Join(
                    ", ",
#pragma warning disable UA2001 // The Banned API Analyzer produces compile errors for any new Linq code. This pre-existing usage has been suppressed, but should be rewritten if possible.
                    GetRemovedComponentDependencies(assetComponent, instanceGameObject, OverrideOperation.Apply).Select(ObjectNames.GetInspectorTitle));
#pragma warning restore UA2001
                if (!string.IsNullOrEmpty(dependentComponents))
                {
                    string error = String.Format(
                        L10n.Tr("Can't apply removed component {0} because {1} component in the Prefab Asset depends on it."),
                        ObjectNames.GetInspectorTitle(assetComponent),
                        dependentComponents);
                    EditorUtility.DisplayDialog(L10n.Tr("Can't apply removed component"), error, L10n.Tr("OK"));
                    return;

View on GitHub (pinned to 225b0fbdb5)

Solutions

  1. Null-check assetComponent before calling: if (assetComponent == null) return;
  2. Resolve the asset component from a fresh prefab asset load (AssetDatabase.LoadAssetAtPath) right before the call.
  3. Ensure you pass the asset-side Component, not the instance-side one.

Example fix

// before
var assetComp = asset.GetComponent<MyBehaviour>();
PrefabUtility.ApplyRemovedComponent(instanceGo, assetComp, InteractionMode.UserAction);

// after
var assetComp = asset.GetComponent<MyBehaviour>();
if (assetComp == null) return;
PrefabUtility.ApplyRemovedComponent(instanceGo, assetComp, InteractionMode.UserAction);
Defensive patterns

Strategy: validation

Validate before calling

if (assetComponent == null) { Debug.LogWarning("Prefab source component is null."); return; }
PrefabUtility.ApplyRemovedComponent(instanceGameObject, assetComponent, action);

Type guard

static bool IsLiveAssetComponent(Component assetComponent)
    => assetComponent != null && EditorUtility.IsPersistent(assetComponent);

Prevention

When it happens

Trigger: Calling ApplyRemovedComponent with a null assetComponent; passing a Component reference that was destroyed; forwarding a lookup (e.g. GetComponent on the asset) that returned null.

Common situations: Editor flows that apply 'removed component' overrides where the asset-side component lookup failed; scripts that compute assetComponent from a stale reference; passing the instance component instead of the asset component by mistake.

Related errors


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