{"record":{"id":"379a4409f567abf3","repo":"Unity-Technologies/UnityCsReference","slug":"calling-apply-or-revert-methods-on-an-object-which","errorCode":null,"errorMessage":"Calling apply or revert methods on an object which is not a GameObject or Component is not supported.","messagePattern":"Calling apply or revert methods on an object which is not a GameObject or Component is not supported\\.","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"Editor/Mono/Prefabs/PrefabUtility.cs","lineNumber":245,"sourceCode":"                    var error = AssetDatabase.ExtractAsset(material, newAssetPath);\n                    if (String.IsNullOrEmpty(error))\n                    {\n                        assetsToReload.Add(importer.assetPath);\n                    }\n                }\n            }\n\n            foreach (var path in assetsToReload)\n            {\n                AssetDatabase.WriteImportSettingsIfDirty(path);\n                AssetDatabase.ImportAsset(path, ImportAssetOptions.ForceUpdate);\n            }\n        }\n\n        static void ThrowExceptionIfNotValidPrefabInstanceObject(Object prefabInstanceObject, bool isApply)\n        {\n            if (!(prefabInstanceObject is GameObject || prefabInstanceObject is Component))\n                throw new ArgumentException(\"Calling apply or revert methods on an object which is not a GameObject or Component is not supported.\", nameof(prefabInstanceObject));\n            if (prefabInstanceObject == null)\n                throw new NullReferenceException(\"Cannot apply or revert object. Object is null.\");\n            if (!PrefabUtility.IsPartOfPrefabInstance(prefabInstanceObject))\n                throw new ArgumentException(\"Calling apply or revert methods on an object which is not part of a Prefab instance is not supported.\", nameof(prefabInstanceObject));\n\n            // We support revert operations on Prefab Assets, but not apply operations.\n            if (isApply)\n                ThrowExceptionIfInstanceIsPersistent(prefabInstanceObject);\n        }\n\n        static void ThrowExceptionIfAllPrefabInstanceObjectsAreInvalid(Object[] prefabInstanceObjects, bool isApply)\n        {\n            foreach (var obj in prefabInstanceObjects)\n            {\n                if (obj != null && (obj is GameObject || obj is Component) && IsPartOfPrefabInstance(obj) && !(isApply && EditorUtility.IsPersistent(obj)))\n                    return;\n            }\n","sourceCodeStart":227,"sourceCodeEnd":263,"githubUrl":"https://github.com/Unity-Technologies/UnityCsReference/blob/225b0fbdb57cc17d094e8056b71f8314aba56f73/Editor/Mono/Prefabs/PrefabUtility.cs#L227-L263","documentation":"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).","triggerScenarios":"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.","commonSituations":"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.","solutions":["Pass only a GameObject or Component that is part of a prefab instance.","Type-check before calling: `if (obj is GameObject || obj is Component)`.","Resolve the correct instance object (e.g. via PrefabUtility.GetCorrespondingObjectFromSource) instead of an asset/other type."],"exampleFix":"// before\nPrefabUtility.ApplyPrefabOverride(someAssetOrMaterial, path);\n\n// after\nif (obj is GameObject || obj is Component)\n    PrefabUtility.ApplyPrefabOverride(obj, path);\nelse\n    Debug.LogWarning($\"{obj} is not a GameObject or Component; skipping.\");","handlingStrategy":"type-guard","validationCode":"if (!(obj is GameObject || obj is Component))\n{ Debug.LogWarning(\"Apply/revert requires a GameObject or Component.\"); return; }","typeGuard":"static bool IsApplyRevertTarget(UnityEngine.Object o) => o is GameObject || o is Component;","tryCatchPattern":null,"preventionTips":["Type-check inputs before calling apply/revert APIs.","Filter generic Object selections to GameObject/Component only.","Note this guard also catches null inputs, so fix nulls upstream to get clearer errors."],"tags":["prefab","apply-revert","type-validation"],"backgroundTag":null,"analyzedSha":"225b0fbdb57cc17d094e8056b71f8314aba56f73","analyzedAt":"2026-08-13T19:07:19.849Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}