Unity-Technologies/UnityCsReference · error · ArgumentException

Cannot apply or revert on any of the objects. Attempt with i

Error message

Cannot apply or revert on any of the objects. Attempt with individual objects for details.

What it means

Thrown by ThrowExceptionIfAllPrefabInstanceObjectsAreInvalid when none of the objects in the input array pass the validity filter (non-null, GameObject/Component, IsPartOfPrefabInstance, and not persistent-when-applying). It is a bulk variant: rather than failing on the first invalid item, Unity reports that the entire batch is unusable and suggests retrying individually for details.

Source

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

                throw new NullReferenceException("Cannot apply or revert object. Object is null.");
            if (!PrefabUtility.IsPartOfPrefabInstance(prefabInstanceObject))
                throw new ArgumentException("Calling apply or revert methods on an object which is not part of a Prefab instance is not supported.", nameof(prefabInstanceObject));

            // We support revert operations on Prefab Assets, but not apply operations.
            if (isApply)
                ThrowExceptionIfInstanceIsPersistent(prefabInstanceObject);
        }

        static void ThrowExceptionIfAllPrefabInstanceObjectsAreInvalid(Object[] prefabInstanceObjects, bool isApply)
        {
            foreach (var obj in prefabInstanceObjects)
            {
                if (obj != null && (obj is GameObject || obj is Component) && IsPartOfPrefabInstance(obj) && !(isApply && EditorUtility.IsPersistent(obj)))
                    return;
            }

            // Throw exception if all objects are invalid
            throw new ArgumentException("Cannot apply or revert on any of the objects. Attempt with individual objects for details.", nameof(prefabInstanceObjects));
        }

        static void ThrowExceptionIfInstanceIsPersistent(Object prefabInstanceObject)
        {
            if (EditorUtility.IsPersistent(prefabInstanceObject))
                throw new ArgumentException("Calling apply methods on an instance which is part of a Prefab Asset is not supported.", nameof(prefabInstanceObject));
        }

        public static GameObject[] FindAllInstancesOfPrefab(GameObject prefabRoot)
        {
            return FindAllInstancesOfPrefab_internal(prefabRoot, SceneHandle.None);
        }

        public static GameObject[] FindAllInstancesOfPrefab(GameObject prefabRoot, Scene scene)
        {
            if (!scene.IsValid())
            {
                throw new ArgumentException("Input scene is not valid: Could not be found.");

View on GitHub (pinned to 225b0fbdb5)

Solutions

  1. Filter the array to valid objects before calling the bulk API.
  2. Retry with individual objects to identify which specific check each fails.
  3. Pre-validate each element with the same predicate Unity uses (non-null + GameObject/Component + IsPartOfPrefabInstance + not persistent for apply).

Example fix

// before
PrefabUtility.ApplyPrefabOverride(allInvalidObjs, path); // throws

// after
var valid = objs.Where(o => o != null && (o is GameObject || o is Component)
    && PrefabUtility.IsPartOfPrefabInstance(o)
    && !EditorUtility.IsPersistent(o)).ToArray();
if (valid.Length == 0) { Debug.LogWarning("No valid objects."); return; }
PrefabUtility.ApplyPrefabOverride(valid, path);
Defensive patterns

Strategy: validation

Validate before calling

var valid = objs.Where(o => o != null && (o is GameObject || o is Component)
    && PrefabUtility.IsPartOfPrefabInstance(o)
    && !(isApply && UnityEditor.EditorUtility.IsPersistent(o))).ToArray();
if (valid.Length == 0) { Debug.LogWarning("No valid objects to apply/revert."); return; }

Prevention

When it happens

Trigger: Calling an array-based apply/revert overload where every element is null, not a GameObject/Component, not part of a prefab instance, or (for apply) persistent/prefab-asset objects.

Common situations: Bulk editor operations on a selection that contains only invalid items. Passing an array of asset-side objects to an apply-all method. Applying to a list where all entries were unpacked.

Related errors


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