Unity-Technologies/UnityCsReference · error · ArgumentException

The object {objectToRemove} (path: {objectToRemovePath}) is

Error message

The object {objectToRemove} (path: {objectToRemovePath}) is not part of the build profile {path}.

What it means

Thrown by RemoveComponent<T>(T objectToRemove) when the object's asset path does not match the build profile's asset path. The method compares AssetDatabase.GetAssetPath(this) against AssetDatabase.GetAssetPath(objectToRemove); only sub-assets of the same profile .asset file are eligible for removal. A mismatch produces an ArgumentException listing both paths.

Source

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

        /// <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.
        /// For internal use only to accommodate required components.
        /// </summary>
        [VisibleToOtherModules]
        internal void ForceRemoveComponent<T>() where T : UnityEngine.Object
        {
            var found = GetComponent<T>();
            if (found == null)
            {

View on GitHub (pinned to 225b0fbdb5)

Solutions

  1. Use GetComponent<T>() to fetch the actual sub-asset belonging to the profile and pass that instance.
  2. Use the parameterless RemoveComponent<T>() which looks up the correct instance internally.
  3. Confirm the object is a sub-asset of the profile via AssetDatabase.GetAssetPath before calling.

Example fix

// before
profile.RemoveComponent(someOtherProfileComponent); // different path

// after
var own = profile.GetComponent<MySettings>();
if (own != null) profile.RemoveComponent(own);
Defensive patterns

Strategy: validation

Validate before calling

string profPath = AssetDatabase.GetAssetPath(profile);
if (AssetDatabase.GetAssetPath(objectToRemove) == profPath) profile.RemoveComponent(objectToRemove);

Type guard

static bool BelongsToProfile(BuildProfile p, UnityEngine.Object o) => AssetDatabase.GetAssetPath(o) == AssetDatabase.GetAssetPath(p);

Prevention

When it happens

Trigger: Passing an object that lives under a different asset file (or is not a sub-asset of this profile) to RemoveComponent. GetAssetPath returns a different path, so the guard fires.

Common situations: Passing a global/shared asset instead of the profile's own sub-asset; mixing up references between profiles; passing a freshly created instance that was never added to the profile.

Related errors


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