Unity-Technologies/UnityCsReference · error · ArgumentNullException

The object to remove cannot be null.

Error message

The object to remove cannot be null.

What it means

Thrown by the specific-object overload RemoveComponent<T>(T objectToRemove) when the passed reference is null. This is a null-guard identical in spirit to the one in AddComponent, throwing ArgumentNullException with the parameter name. Because T is constrained to UnityEngine.Object, destroyed objects (fake-null) are also caught.

Source

Thrown at Editor/Mono/BuildProfile/BuildProfile.API.cs:158

            }

            if (Array.IndexOf(this.requiredComponents, found) > -1)
                throw new ArgumentException($"The component {typeof(T).Name} is required and cannot be removed from the build profile {name}.");

            AssetDatabase.RemoveObjectFromAsset(found);
        }

        /// <summary>
        /// Removes a specific component from the build profile.
        /// </summary>
        /// <param name="objectToRemove">Object to remove.</param>
        /// <exception cref="ArgumentNullException">objectToRemove is null.</exception>
        /// <exception cref="ArgumentException">objectToRemove is not part of the build profile.</exception>
        public void RemoveComponent<T>(T objectToRemove) where T : UnityEngine.Object
        {
            if (objectToRemove == null)
            {
                throw new ArgumentNullException(nameof(objectToRemove), "The object to remove cannot be null.");
            }

            var path = AssetDatabase.GetAssetPath(this);
            var objectToRemovePath = AssetDatabase.GetAssetPath(objectToRemove);

            if (objectToRemovePath != path)
            {
                throw new ArgumentException($"The object {objectToRemove} (path: {objectToRemovePath}) is not part of the build profile {path}.");
            }

            if (Array.IndexOf(this.requiredComponents, objectToRemove) > -1)
                throw new ArgumentException($"The component {typeof(T).Name} is required and cannot be removed from the build profile {name}.");

            AssetDatabase.RemoveObjectFromAsset(objectToRemove);
        }

        /// <summary>
        /// Forcibly removes the first occurrence of a component of type T from the build profile.

View on GitHub (pinned to 225b0fbdb5)

Solutions

  1. Null-check objectToRemove before calling RemoveComponent.
  2. Verify the object is still valid (e.g. objectToRemove != null) and part of the profile before attempting removal.
  3. Use the parameterless RemoveComponent<T>() if you only know the type, not a specific instance.

Example fix

// before
profile.RemoveComponent(maybeNullRef);

// after
if (objectToRemove != null)
    profile.RemoveComponent(objectToRemove);
Defensive patterns

Strategy: validation

Validate before calling

if (objectToRemove != null) profile.RemoveComponent(objectToRemove);

Type guard

static bool IsLive(UnityEngine.Object o) => o != null;

Prevention

When it happens

Trigger: Passing a null or destroyed UnityEngine.Object to RemoveComponent(objectToRemove). Happens when a cached reference goes stale after AssetDatabase operations or when a field was never assigned.

Common situations: Calling RemoveComponent with a reference cached before an asset reload that has since destroyed the object; logic that does not verify the object still exists before removal; iterating a list that may contain nulls.

Related errors


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