{"record":{"id":"c6ab4f1315d2c770","repo":"Unity-Technologies/UnityCsReference","slug":"replacing-the-root-prefab-instance-in-a-variant-is","errorCode":null,"errorMessage":"Replacing the root Prefab instance in a Variant is not supported since it will break all overrides for existing instances of this Variant, including their positions and rotations.","messagePattern":"Replacing the root Prefab instance in a Variant is not supported since it will break all overrides for existing instances of this Variant, including their positions and rotations\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"Editor/Mono/Prefabs/PrefabUtility.cs","lineNumber":2257,"sourceCode":"            if (prefabInstanceRoot == null)\n                throw new ArgumentNullException(nameof(prefabInstanceRoot));\n\n            if (prefabAssetRoot == null)\n                throw new ArgumentNullException(nameof(prefabAssetRoot));\n\n            if (checkValidAsset)\n                ThrowIfInvalidAssetForReplacePrefabInstance(prefabAssetRoot, mode);\n\n            if (!IsPartOfNonAssetPrefabInstance(prefabInstanceRoot))\n                throw new InvalidOperationException(string.Format(\"Input '{0}' is not a Prefab instance, for plain GameObjects use ConvertToPrefabInstance() instead\", prefabInstanceRoot.name));\n\n            if (!IsOutermostPrefabInstanceRoot(prefabInstanceRoot))\n                throw new ArgumentException(\"Input instance is not an outermost Prefab instance root. Input instance: \" + prefabInstanceRoot.name, nameof(prefabInstanceRoot));\n            if (EditorUtility.IsPersistent(prefabInstanceRoot))\n                throw new ArgumentException(\"Input instance root is from a Prefab asset, this is not supported. Input instance: \" + prefabInstanceRoot.name, nameof(prefabInstanceRoot));\n\n            if (PrefabStageUtility.IsGameObjectThePrefabRootInAnyPrefabStage(prefabInstanceRoot))\n                throw new InvalidOperationException(\"Replacing the root Prefab instance in a Variant is not supported since it will break all overrides for existing instances of this Variant, including their positions and rotations.\" + prefabInstanceRoot.name);\n            if (IsAnyPrefabInstanceRoot(prefabInstanceRoot) && EditorSceneManager.IsPreviewSceneObject(prefabInstanceRoot) && prefabInstanceRoot.transform.parent == null) // EditPrefabContentsScope handling\n                throw new InvalidOperationException(\"Replacing the Variant parent is not supported since it will break all overrides for existing instances of this Variant, including their positions and rotations.\" + prefabInstanceRoot.name);\n            if (prefabInstanceRoot.transform.GetType() != prefabAssetRoot.transform.GetType())\n                throw new InvalidOperationException(string.Format(\"Cannot replace the Prefab instance '{0}' with root transform of type {1} with a Prefab asset with root transform of type {2}. Transform types must match.\", prefabInstanceRoot.name, prefabInstanceRoot.transform.GetType().Name, prefabAssetRoot.transform.GetType().Name));\n\n            if (prefabInstanceRoot.hideFlags.HasFlag(HideFlags.DontSaveInEditor) || prefabInstanceRoot.transform.hideFlags.HasFlag(HideFlags.DontSaveInEditor))\n                throw new ArgumentException(\"Input instance root is using the HideFlags.DontSaveInEditor flag which is not supported when replacing: Input instance: \" + prefabInstanceRoot.name, nameof(prefabInstanceRoot));\n\n            if (mode == InteractionMode.UserAction)\n            {\n                // Recording undo does not handle missing scripts\n                var gameObjectsWithInvalidScript = FindGameObjectsWithInvalidComponent(prefabInstanceRoot);\n                if (gameObjectsWithInvalidScript.Count > 0)\n                    throw new InvalidOperationException(string.Format($\"Cannot replace the Prefab instance when it has a missing script. GameObject '{gameObjectsWithInvalidScript[0].name}' has a missing script. Use InteractionMode.AutomatedAction to force the replace.\"));\n            }\n        }\n\n        public static void ReplacePrefabAssetOfPrefabInstances(GameObject[] prefabInstanceRoots, GameObject prefabAssetRoot, InteractionMode mode)","sourceCodeStart":2239,"sourceCodeEnd":2275,"githubUrl":"https://github.com/Unity-Technologies/UnityCsReference/blob/225b0fbdb57cc17d094e8056b71f8314aba56f73/Editor/Mono/Prefabs/PrefabUtility.cs#L2239-L2275","documentation":"Thrown by ThrowIfInvalidArgumentsForReplacePrefabInstance when PrefabStageUtility.IsGameObjectThePrefabRootInAnyPrefabStage(prefabInstanceRoot) is true. This means the object is the root being edited inside an open Prefab Stage (Prefab Mode). Replacing the root of a Prefab/Variant being edited would silently destroy all overrides on downstream instances, so Unity blocks it.","triggerScenarios":"User opens a Variant Prefab in Prefab Mode (double-click to edit) and a script then calls ReplacePrefabAssetOfPrefabInstance on the Prefab Stage root. Automated build pipeline that opens Prefab Stage via PrefabStageUtility and tries to swap the root.","commonSituations":"Editor scripts triggered by PostProcessScene or scene-save callbacks while a Prefab Stage is open. Batch operations that don't check whether the current selection is inside a Prefab Stage.","solutions":["Close the Prefab Stage (StageUtility.GoToMainStage) before calling ReplacePrefabAssetOfPrefabInstance.","Guard with: if (PrefabStageUtility.IsGameObjectThePrefabRootInAnyPrefabStage(obj)) return;","Edit the Prefab asset contents via PrefabUtility.LoadPrefabContents and operate there instead of in a live Stage."],"exampleFix":"// before\nPrefabUtility.ReplacePrefabAssetOfPrefabInstance(stageRootGo, prefabAsset, mode);\n\n// after\nStageUtility.GoToMainStage(); // close the open Prefab Stage first\nPrefabUtility.ReplacePrefabAssetOfPrefabInstance(stageRootGo, prefabAsset, mode);","handlingStrategy":"validation","validationCode":"if (PrefabStageUtility.IsGameObjectThePrefabRootInAnyPrefabStage(prefabInstanceRoot))\n{\n    StageUtility.GoToMainStage(); // close the Prefab Stage\n}\nPrefabUtility.ReplacePrefabAssetOfPrefabInstance(prefabInstanceRoot, prefabAssetRoot, mode);","typeGuard":"static bool IsSafeToReplace(GameObject obj)\n{\n    return obj != null\n        && !PrefabStageUtility.IsGameObjectThePrefabRootInAnyPrefabStage(obj);\n}","tryCatchPattern":null,"preventionTips":["Check whether a Prefab Stage is open before running batch Prefab operations.","Register callbacks (e.g. EditorApplication.delayCall) only when the main stage is active."],"tags":["prefab","prefab-stage","variant","unity-editor","argument-validation"],"backgroundTag":null,"analyzedSha":"225b0fbdb57cc17d094e8056b71f8314aba56f73","analyzedAt":"2026-08-13T19:07:19.849Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}