Unity-Technologies/UnityCsReference · error · ArgumentException

The component {typeof(T).Name} is required and cannot be rem

Error message

The component {typeof(T).Name} is required and cannot be removed from the build profile {name}.

What it means

Thrown by the parameterless RemoveComponent<T>() when the located component is listed in the profile's requiredComponents array. Required components are protected from removal; the method uses Array.IndexOf(this.requiredComponents, found) > -1 to detect this and throws ArgumentException. Use ForceRemoveComponent<T>() (internal) to bypass the guard.

Source

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

            // TODO: Future API will support build profile components as reference.
            throw new InvalidOperationException($"Asset {objectToAdd} has a non-null path. Build profile components should be sub-assets.");
        }

        /// <summary>
        /// Removes the first occurence of a component of type T from the build profile.
        /// </summary>
        public void RemoveComponent<T>() where T : UnityEngine.Object
        {
            var found = GetComponent<T>();
            if (found == null)
            {
                Debug.LogWarning($"The component {typeof(T).Name} does not exist in the build profile {name}. ");
                return;
            }

            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);

View on GitHub (pinned to 225b0fbdb5)

Solutions

  1. Do not remove the required component — instead update its fields/config in place.
  2. If removal is genuinely necessary from internal module code, use ForceRemoveComponent<T>() (internal, VisibleToOtherModules).
  3. Inspect profile.requiredComponents to know which types are protected before attempting removal.

Example fix

// before
profile.RemoveComponent<RequiredSettings>(); // throws

// after
// Update the required component in place instead of removing it.
var s = profile.GetComponent<RequiredSettings>();
s.someField = newValue;
Defensive patterns

Strategy: validation

Validate before calling

var existing = profile.GetComponent<T>();
if (existing != null && Array.IndexOf(profile.requiredComponents, existing) < 0) profile.RemoveComponent<T>();

Type guard

static bool IsRemovable<T>(BuildProfile p) where T : UnityEngine.Object { var c = p.GetComponent<T>(); return c != null && Array.IndexOf(p.requiredComponents, c) < 0; }

Prevention

When it happens

Trigger: Calling RemoveComponent<T>() on a type that is registered as required for the build profile (e.g. a platform-specific required component). The component is found, but it is in the requiredComponents array.

Common situations: Attempting to clean up or reset a profile and removing a component the platform marks as mandatory; scripting logic that tries to remove all components generically; misunderstanding which components are required for a given platform.

Related errors


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