{"record":{"id":"1302b0416693b961","repo":"Unity-Technologies/UnityCsReference","slug":"prefab-instance-must-match-prefab-source","errorCode":null,"errorMessage":"Prefab instance must match Prefab source.","messagePattern":"Prefab instance must match Prefab source\\.","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"Editor/Mono/Prefabs/PrefabUtility.cs","lineNumber":1568,"sourceCode":"        private static void RemoveRemovedGameObjectOverridesWhichAreNull(Object prefabInstanceObject)\n        {\n            var removedGameObjects = PrefabUtility.GetRemovedGameObjects(prefabInstanceObject);\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 != null select go).ToArray();\n#pragma warning restore UA2001\n            PrefabUtility.SetRemovedGameObjects(prefabInstanceObject, filteredRemovedGameObjects);\n        }\n\n        public static void ApplyRemovedGameObject(GameObject gameObjectInInstance, GameObject assetGameObject, InteractionMode action)\n        {\n            DateTime startTime = DateTime.UtcNow;\n\n            ThrowExceptionIfNotValidPrefabInstanceObject(gameObjectInInstance, true);\n\n            if (assetGameObject == null)\n                throw new ArgumentNullException(nameof(assetGameObject), \"Prefab source must not be null.\");\n            if (!IsPrefabInstanceObjectOf(gameObjectInInstance, PrefabUtility.GetPrefabAssetHandle(assetGameObject)))\n                throw new ArgumentException(\"Prefab instance must 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 = \"Apply Prefab removed GameObject\";\n            var prefabInstanceObject = PrefabUtility.GetPrefabInstanceHandle(gameObjectInInstance);\n            GameObject prefabInstanceRoot = GetOutermostPrefabInstanceRoot(gameObjectInInstance);\n\n            if (prefabInstanceObject == null)\n                throw new ArgumentNullException(nameof(prefabInstanceObject), \"Prefab instance must not be null.\");\n\n            PrefabUtility.Internal_CallPrefabInstanceApplying(prefabInstanceRoot);\n\n            string assetPath = AssetDatabase.GetAssetPath(assetGameObject);\n            GameObject assetRoot = GetRootGameObject(assetGameObject);\n            byte[] originalFileContent = null;\n\n            if (action == InteractionMode.UserAction)\n            {","sourceCodeStart":1550,"sourceCodeEnd":1586,"githubUrl":"https://github.com/Unity-Technologies/UnityCsReference/blob/225b0fbdb57cc17d094e8056b71f8314aba56f73/Editor/Mono/Prefabs/PrefabUtility.cs#L1550-L1586","documentation":"Thrown by ApplyRemovedGameObject when IsPrefabInstanceObjectOf(gameObjectInInstance, PrefabUtility.GetPrefabAssetHandle(assetGameObject)) returns false — i.e., the instance GameObject does not belong to the same prefab asset as the supplied assetGameObject. Unity requires the instance and the asset to be the same prefab lineage to map the removed-child override.","triggerScenarios":"Passing an instance GameObject from prefab A with an assetGameObject from prefab B; passing an instance that is not actually a prefab instance; passing an assetGameObject that is unrelated to the instance's source asset.","commonSituations":"Bulk apply tools that pair instance and asset objects from mismatched prefabs; editor code that grabs the wrong asset reference when multiple prefabs are in play; refactors that mixed up instance vs asset GameObject arguments.","solutions":["Verify the pairing: require PrefabUtility.GetPrefabAssetHandle(assetGameObject) equals the instance's asset handle before calling.","Resolve assetGameObject from the instance's own source asset (GetCorrespondingObjectFromSource) so lineage matches by construction.","Ensure gameObjectInInstance is part of a prefab instance and assetGameObject is from that same instance's asset root."],"exampleFix":"// before\nPrefabUtility.ApplyRemovedGameObject(instanceGo, someAssetGo, InteractionMode.UserAction);\n\n// after\nvar assetRoot = PrefabUtility.GetPrefabAssetHandle(someAssetGo);\nif (PrefabUtility.IsPrefabInstanceObjectOf(instanceGo, assetRoot))\n    PrefabUtility.ApplyRemovedGameObject(instanceGo, someAssetGo, InteractionMode.UserAction);","handlingStrategy":"validation","validationCode":"var assetHandle = PrefabUtility.GetPrefabAssetHandle(assetGameObject);\nif (!PrefabUtility.IsPrefabInstanceObjectOf(gameObjectInInstance, assetHandle))\n{ Debug.LogWarning(\"Instance does not match the prefab source.\"); return; }\nPrefabUtility.ApplyRemovedGameObject(gameObjectInInstance, assetGameObject, action);","typeGuard":"static bool InstanceMatchesAsset(GameObject instanceGo, GameObject assetGo)\n    => assetGo != null && instanceGo != null &&\n       PrefabUtility.IsPrefabInstanceObjectOf(instanceGo, PrefabUtility.GetPrefabAssetHandle(assetGo));","tryCatchPattern":null,"preventionTips":["Pair instance and asset objects from the same prefab lineage.","Resolve assetGameObject from the instance's own source asset via GetCorrespondingObjectFromSource.","Validate IsPrefabInstanceObjectOf before applying removed-child overrides."],"tags":["prefab","validation","lineage-mismatch","removed-gameobject"],"backgroundTag":null,"analyzedSha":"225b0fbdb57cc17d094e8056b71f8314aba56f73","analyzedAt":"2026-08-13T19:07:19.849Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}