Unity-Technologies/UnityCsReference · error · ArgumentNullException
Cannot apply added component. Component is null.
Error message
Cannot apply added component. Component is null.
What it means
Thrown by ApplyAddedComponent(Component, string, InteractionMode) when component is null — ArgumentNullException(nameof(component)). The method dereferences component immediately (IsAddedComponentOverride, gameObject access), so null is rejected with an explanatory message.
Source
Thrown at Editor/Mono/Prefabs/PrefabUtility.cs:1168
{
if (originalComponentOrder.Length != newComponentOrder.Length)
return true;
for (int i = 0; i < originalComponentOrder.Length; ++i)
{
if (originalComponentOrder[i] != newComponentOrder[i])
return true;
}
return false;
}
public static void ApplyAddedComponent(Component component, string assetPath, InteractionMode action)
{
DateTime startTime = DateTime.UtcNow;
if (component == null)
throw new ArgumentNullException(nameof(component), "Cannot apply added component. Component is null.");
if (!PrefabUtility.IsAddedComponentOverride(component))
throw new ArgumentException("Cannot apply added component. Component is not an added component override on a Prefab instance.", nameof(component));
ThrowExceptionIfInstanceIsPersistent(component);
try
{
GameObject prefabSourceGameObject = GetCorrespondingObjectFromSourceAtPath(component.gameObject, assetPath);
if (prefabSourceGameObject == null)
return;
var originalComponentOrder = component.gameObject.GetComponents<Component>();
var actionName = "Apply Added Component";
if (action == InteractionMode.UserAction)
{
string dependentComponents = string.Join(View on GitHub (pinned to 225b0fbdb5)
Solutions
- Null-check before calling: if (component == null) return;
- Verify GetComponent returned a valid component before forwarding.
- Use the Unity null check (component == null) which also catches destroyed UnityEngine.Object references.
Example fix
// before var c = go.GetComponent<Rigidbody>(); PrefabUtility.ApplyAddedComponent(c, assetPath, InteractionMode.UserAction); // after var c = go.GetComponent<Rigidbody>(); if (c == null) return; PrefabUtility.ApplyAddedComponent(c, assetPath, InteractionMode.UserAction);
Defensive patterns
Strategy: validation
Validate before calling
if (component == null) { Debug.LogWarning("Component is null; cannot apply."); return; }
PrefabUtility.ApplyAddedComponent(component, assetPath, action); Type guard
static bool IsLiveComponent(Component c) => c != null && c.gameObject != null;
Prevention
- Null-check (Unity null check) any Component before apply/revert APIs.
- Re-resolve components via GetComponent immediately before the call.
- Guard editor menu items against empty selections.
When it happens
Trigger: Calling ApplyAddedComponent(null, ...); passing a Component reference that was destroyed before the call; GetComponent<T>() returning null and being forwarded.
Common situations: Editor menu items that apply the currently selected component when nothing valid is selected; GetComponent lookups that silently return null; deferred apply operations whose target was destroyed.
Related errors
- Cannot revert added component. Component is null.
- instanceRoots
- prefabInstances
- Cannot apply added component. Component is not an added comp
- Cannot revert added component. Component is not an added com
AI-assisted analysis of Unity-Technologies/UnityCsReference@225b0fbdb5 (2026-08-13).
Data as JSON: /api/errors/dcf24d6ac2bccf5a.
Report an issue: GitHub.