{"record":{"id":"cfd36d9bb8b4b15a","repo":"Unity-Technologies/UnityCsReference","slug":"calling-apply-or-revert-methods-on-an-object-which-cfd36d","errorCode":null,"errorMessage":"Calling apply or revert methods on an object which is not part of a Prefab instance is not supported.","messagePattern":"Calling apply or revert methods on an object which is not part of a Prefab instance is not supported\\.","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"Editor/Mono/Prefabs/PrefabUtility.cs","lineNumber":249,"sourceCode":"                    }\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\n            // Throw exception if all objects are invalid\n            throw new ArgumentException(\"Cannot apply or revert on any of the objects. Attempt with individual objects for details.\", nameof(prefabInstanceObjects));\n        }\n","sourceCodeStart":231,"sourceCodeEnd":267,"githubUrl":"https://github.com/Unity-Technologies/UnityCsReference/blob/225b0fbdb57cc17d094e8056b71f8314aba56f73/Editor/Mono/Prefabs/PrefabUtility.cs#L231-L267","documentation":"Thrown by ThrowExceptionIfNotValidPrefabInstanceObject when the object is a non-null GameObject/Component but is NOT part of a prefab instance (PrefabUtility.IsPartOfPrefabInstance returns false). Apply/revert operate on overrides within prefab instances, so a plain scene object or an unpacked object is invalid.","triggerScenarios":"Passing a regular scene GameObject with no prefab connection, an unpacked prefab, or an object whose instance link was stripped. Passing a prefab asset (which is not an 'instance').","commonSituations":"Operating on objects after PrefabUtility.UnpackPrefabInstance severed the link. Selecting plain scene objects in a custom apply tool. Forgetting that revert on a prefab asset is allowed but apply is not.","solutions":["Ensure the object is part of a prefab instance: check IsPartOfPrefabInstance first.","If unpacked, reconnect via InstantiatePrefab or reapply to a valid instance.","For revert-on-asset flows use the asset path variant; for apply, operate on an instance in a scene."],"exampleFix":"// before\nPrefabUtility.ApplyPrefabOverride(unpackedGo, path);\n\n// after\nif (!PrefabUtility.IsPartOfPrefabInstance(obj))\n{ Debug.LogWarning(\"Object is not part of a prefab instance.\"); return; }\nPrefabUtility.ApplyPrefabOverride(obj, path);","handlingStrategy":"validation","validationCode":"if (!PrefabUtility.IsPartOfPrefabInstance(obj))\n{ Debug.LogWarning(\"Object is not part of a prefab instance.\"); return; }","typeGuard":"static bool IsPrefabInstanceObject(UnityEngine.Object o) => o is GameObject || o is Component && PrefabUtility.IsPartOfPrefabInstance(o);","tryCatchPattern":null,"preventionTips":["Always check IsPartOfPrefabInstance before apply/revert.","Do not unpack prefab instances you still need to apply overrides to.","Use the asset-path revert overload for asset-side reverts, not instance apply."],"tags":["prefab","apply-revert","instance-validation"],"backgroundTag":null,"analyzedSha":"225b0fbdb57cc17d094e8056b71f8314aba56f73","analyzedAt":"2026-08-13T19:07:19.849Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}