Unity-Technologies/UnityCsReference · error · ArgumentException

Calling apply or revert methods on an object which is not a

Error message

Calling apply or revert methods on an object which is not a GameObject or Component is not supported.

What it means

Thrown by ThrowExceptionIfNotValidPrefabInstanceObject (used by apply/revert APIs) when the passed object is neither a GameObject nor a Component. Apply/revert operations only make sense on scene instance objects of those types. This is the FIRST check, so it fires even for null inputs (since `null is GameObject` is false).

Source

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

                    var error = AssetDatabase.ExtractAsset(material, newAssetPath);
                    if (String.IsNullOrEmpty(error))
                    {
                        assetsToReload.Add(importer.assetPath);
                    }
                }
            }

            foreach (var path in assetsToReload)
            {
                AssetDatabase.WriteImportSettingsIfDirty(path);
                AssetDatabase.ImportAsset(path, ImportAssetOptions.ForceUpdate);
            }
        }

        static void ThrowExceptionIfNotValidPrefabInstanceObject(Object prefabInstanceObject, bool isApply)
        {
            if (!(prefabInstanceObject is GameObject || prefabInstanceObject is Component))
                throw new ArgumentException("Calling apply or revert methods on an object which is not a GameObject or Component is not supported.", nameof(prefabInstanceObject));
            if (prefabInstanceObject == null)
                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;
            }

View on GitHub (pinned to 225b0fbdb5)

Solutions

  1. Pass only a GameObject or Component that is part of a prefab instance.
  2. Type-check before calling: `if (obj is GameObject || obj is Component)`.
  3. Resolve the correct instance object (e.g. via PrefabUtility.GetCorrespondingObjectFromSource) instead of an asset/other type.

Example fix

// before
PrefabUtility.ApplyPrefabOverride(someAssetOrMaterial, path);

// after
if (obj is GameObject || obj is Component)
    PrefabUtility.ApplyPrefabOverride(obj, path);
else
    Debug.LogWarning($"{obj} is not a GameObject or Component; skipping.");
Defensive patterns

Strategy: type-guard

Validate before calling

if (!(obj is GameObject || obj is Component))
{ Debug.LogWarning("Apply/revert requires a GameObject or Component."); return; }

Type guard

static bool IsApplyRevertTarget(UnityEngine.Object o) => o is GameObject || o is Component;

Prevention

When it happens

Trigger: Passing an Asset, ScriptableObject, Material, Texture, or any non-GameObject/Component to an apply/revert method. Passing null also lands here because the `is` check precedes the null check.

Common situations: Feeding the wrong object type from a generic editor utility, a Selection that includes non-GameObject objects, or passing a prefab asset handle rather than an instance object. Passing null due to a failed lookup.

Related errors


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