{"record":{"id":"b124236be6b8e07e","repo":"Unity-Technologies/UnityCsReference","slug":"replacing-the-variant-parent-is-not-supported-sinc","errorCode":null,"errorMessage":"Replacing the Variant parent is not supported since it will break all overrides for existing instances of this Variant, including their positions and rotations.","messagePattern":"Replacing the Variant parent 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":2259,"sourceCode":"\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)\n        {\n            ReplacePrefabAssetOfPrefabInstances(prefabInstanceRoots, prefabAssetRoot, new PrefabReplacingSettings(), mode);","sourceCodeStart":2241,"sourceCodeEnd":2277,"githubUrl":"https://github.com/Unity-Technologies/UnityCsReference/blob/225b0fbdb57cc17d094e8056b71f8314aba56f73/Editor/Mono/Prefabs/PrefabUtility.cs#L2241-L2277","documentation":"Thrown by ThrowIfInvalidArgumentsForReplacePrefabInstance when IsAnyPrefabInstanceRoot(prefabInstanceRoot) && EditorSceneManager.IsPreviewSceneObject(prefabInstanceRoot) && prefabInstanceRoot.transform.parent == null — i.e. the object is an unparented Prefab instance root in a Preview Scene (the EditPrefabContentsScope pattern). Replacing a Variant's parent under these conditions would break all overrides on existing instances of that Variant.","triggerScenarios":"Using PrefabUtility.EditPrefabContentsScope (or the older LoadPrefabContents) to open a Variant asset, then calling ReplacePrefabAssetOfPrefabInstance on the root of the loaded contents. Attempting to swap a Variant's parent Prefab inside a preview/edit context.","commonSituations":"Scripted Variant refactoring that loads Variant contents and tries to repoint the base Prefab. Batch Variant migration tools that don't account for the preview-scene constraint.","solutions":["Instead of replacing the Variant parent via EditPrefabContentsScope, create a new Variant from the desired base using SaveAsPrefabAsset with the variant flag.","Operate on a live scene instance of the Variant (not a preview-scene root) if you need ReplacePrefabAssetOfPrefabInstance.","Guard: check EditorSceneManager.IsPreviewSceneObject(obj) and skip or take an alternate path."],"exampleFix":"// before\nusing (var scope = new PrefabUtility.EditPrefabContentsScope(variantPath))\n{\n    PrefabUtility.ReplacePrefabAssetOfPrefabInstance(scope.prefabContentsRoot, newBaseAsset, mode);\n}\n\n// after — recreate the Variant from the new base instead\nvar contents = PrefabUtility.LoadPrefabContents(variantPath);\nvar newPath = AssetDatabase.GenerateUniqueAssetPath(\"Assets/NewVariant.prefab\");\nPrefabUtility.SaveAsPrefabAsset(contents, newPath, out bool success);","handlingStrategy":"validation","validationCode":"if (IsAnyPrefabInstanceRoot(prefabInstanceRoot)\n    && EditorSceneManager.IsPreviewSceneObject(prefabInstanceRoot)\n    && prefabInstanceRoot.transform.parent == null)\n{\n    Debug.LogWarning(\"Cannot replace Variant parent in a preview scene; recreate the Variant instead.\");\n    return;\n}\nPrefabUtility.ReplacePrefabAssetOfPrefabInstance(prefabInstanceRoot, prefabAssetRoot, mode);","typeGuard":"static bool IsPreviewSceneRoot(GameObject obj)\n{\n    return obj != null\n        && EditorSceneManager.IsPreviewSceneObject(obj)\n        && obj.transform.parent == null;\n}","tryCatchPattern":null,"preventionTips":["Avoid calling ReplacePrefabAssetOfPrefabInstance inside EditPrefabContentsScope on the root.","For Variant parent changes, recreate the Variant via SaveAsPrefabAsset instead."],"tags":["prefab","variant","preview-scene","edit-prefab-contents","unity-editor"],"backgroundTag":null,"analyzedSha":"225b0fbdb57cc17d094e8056b71f8314aba56f73","analyzedAt":"2026-08-13T19:07:19.849Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}