{"record":{"id":"fde8068babe2055a","repo":"Unity-Technologies/UnityCsReference","slug":"prefab-instance-should-match-prefab-source","errorCode":null,"errorMessage":"Prefab instance should match Prefab source.","messagePattern":"Prefab instance should match Prefab source\\.","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"Editor/Mono/Prefabs/PrefabUtility.cs","lineNumber":1781,"sourceCode":"            }\n\n            if (index != -1)\n            {\n#pragma warning disable UA2001 // The Banned API Analyzer produces compile errors for any new Linq code. This pre-existing usage has been suppressed, but should be rewritten if possible.\n                var filteredRemovedGameObjects = (from go in removedGameObjects where go != removedGameObjects[index] select go).ToArray();\n#pragma warning restore UA2001\n                PrefabUtility.SetRemovedGameObjects(instanceObject, filteredRemovedGameObjects);\n            }\n        }\n\n        public static void RevertRemovedGameObject(GameObject gameObjectInInstance, GameObject assetGameObject, InteractionMode action)\n        {\n            ThrowExceptionIfNotValidPrefabInstanceObject(gameObjectInInstance, false);\n\n            if (assetGameObject == null)\n                throw new ArgumentNullException(nameof(assetGameObject), \"Prefab source may not be null.\");\n            if (!IsPrefabInstanceObjectOf(gameObjectInInstance, PrefabUtility.GetPrefabAssetHandle(assetGameObject)))\n                throw new ArgumentException(\"Prefab instance should match Prefab source.\");\n            if (assetGameObject.transform.root == assetGameObject.transform)\n                throw new ArgumentException(\"The asset GameObject cannot be the root as the root cannot be removed as an override.\");\n\n            var actionName = \"Revert Prefab removed GameObject\";\n            var prefabInstanceHandle = PrefabUtility.GetPrefabInstanceHandle(gameObjectInInstance);\n\n            GameObject prefabInstanceRoot = GetOutermostPrefabInstanceRoot(gameObjectInInstance);\n\n            if (action == InteractionMode.UserAction)\n                Undo.RegisterFullObjectHierarchyUndo(prefabInstanceRoot, actionName);\n\n            RemoveRemovedGameObjectOverride(prefabInstanceHandle, assetGameObject);\n        }\n\n        public static void RevertAddedGameObject(GameObject gameObject, InteractionMode action)\n        {\n            if (gameObject == null)\n                throw new ArgumentNullException(nameof(gameObject), \"Cannot revert added GameObject. GameObject is null.\");","sourceCodeStart":1763,"sourceCodeEnd":1799,"githubUrl":"https://github.com/Unity-Technologies/UnityCsReference/blob/225b0fbdb57cc17d094e8056b71f8314aba56f73/Editor/Mono/Prefabs/PrefabUtility.cs#L1763-L1799","documentation":"RevertRemovedGameObject verifies that gameObjectInInstance corresponds to assetGameObject's prefab handle via IsPrefabInstanceObjectOf. If the two belong to different prefabs (or one is not actually an instance object of the other's asset), the pairing is invalid and an ArgumentException is thrown.","triggerScenarios":"Passing a gameObjectInInstance and assetGameObject that originate from different prefab assets, or where the instance object no longer maps to the supplied source.","commonSituations":"Iterating removed overrides across multiple prefabs and cross-pairing instance objects with the wrong asset objects; prefab asset swapped/reimported between capture and revert.","solutions":["Pair arguments using GetCorrespondingObjectFromSource so both refer to the same prefab asset.","Ensure both arguments were obtained from the same prefab asset handle (GetPrefabAssetHandle matches).","Rebuild the instance/source mapping from the current prefab state before reverting."],"exampleFix":"// before\nPrefabUtility.RevertRemovedGameObject(goInInstance, assetGo, mode);\n\n// after\nvar assetGo = PrefabUtility.GetCorrespondingObjectFromSource(goInInstance);\nif (assetGo != null && PrefabUtility.IsPrefabInstanceObjectOf(goInInstance, PrefabUtility.GetPrefabAssetHandle(assetGo)))\n    PrefabUtility.RevertRemovedGameObject(goInInstance, assetGo, mode);","handlingStrategy":"validation","validationCode":"var assetGo = PrefabUtility.GetCorrespondingObjectFromSource(gameObjectInInstance);\nif (assetGo == null) return;\nif (!PrefabUtility.IsPrefabInstanceObjectOf(gameObjectInInstance, PrefabUtility.GetPrefabAssetHandle(assetGo))) return;","typeGuard":"static bool InstanceMatchesSource(GameObject instanceGo, GameObject assetGo) =>\n    assetGo != null && PrefabUtility.IsPrefabInstanceObjectOf(instanceGo,\n        PrefabUtility.GetPrefabAssetHandle(assetGo));","tryCatchPattern":null,"preventionTips":["Derive both arguments from the same prefab asset so the pairing is guaranteed.","Use GetCorrespondingObjectFromSource to obtain the matching asset object for a given instance object.","Rebuild the instance/source mapping from current prefab state before reverting across multiple prefabs."],"tags":["prefab","unity","mismatch","validation"],"backgroundTag":null,"analyzedSha":"225b0fbdb57cc17d094e8056b71f8314aba56f73","analyzedAt":"2026-08-13T19:07:19.849Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}