{"record":{"id":"2c2aa1ac6098879f","repo":"Unity-Technologies/UnityCsReference","slug":"input-instance-root-is-using-the-hideflags-dontsav","errorCode":null,"errorMessage":"Input instance root is using the HideFlags.DontSaveInEditor flag which is not supported when replacing: Input instance: ","messagePattern":"Input instance root is using the HideFlags\\.DontSaveInEditor flag which is not supported when replacing: Input instance: ","errorType":"validation","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"Editor/Mono/Prefabs/PrefabUtility.cs","lineNumber":2264,"sourceCode":"                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);\n        }\n\n        public static void ReplacePrefabAssetOfPrefabInstances(GameObject[] prefabInstanceRoots, GameObject prefabAssetRoot, PrefabReplacingSettings settings, InteractionMode mode)\n        {\n            if (prefabInstanceRoots == null)","sourceCodeStart":2246,"sourceCodeEnd":2282,"githubUrl":"https://github.com/Unity-Technologies/UnityCsReference/blob/225b0fbdb57cc17d094e8056b71f8314aba56f73/Editor/Mono/Prefabs/PrefabUtility.cs#L2246-L2282","documentation":"Thrown by ThrowIfInvalidArgumentsForReplacePrefabInstance when prefabInstanceRoot.hideFlags or its transform's hideFlags has HideFlags.DontSaveInEditor set. Objects marked DontSaveInEditor are not serialized to the scene/Prefab, so replacing their Prefab asset would produce inconsistent state. This guard rejects such objects before the replace.","triggerScenarios":"Passing an editor-only helper GameObject (created with HideFlags.DontSaveInEditor so it doesn't persist) that happens to be a Prefab instance. Custom editor tooling that creates transient Prefab instances with DontSaveInEditor and then tries to replace them.","commonSituations":"Gizmo/preview objects created at runtime in editor with DontSaveInEditor. Objects spawned by editor extensions that intentionally avoid serialization but are accidentally flagged as Prefab instances.","solutions":["Remove HideFlags.DontSaveInEditor from the instance root (and its transform) before calling ReplacePrefabAssetOfPrefabInstance.","Operate on a normal scene instance that is serialized normally.","Guard: if (obj.hideFlags.HasFlag(HideFlags.DontSaveInEditor)) return;"],"exampleFix":"// before\nPrefabUtility.ReplacePrefabAssetOfPrefabInstance(instance, asset, mode);\n// instance has HideFlags.DontSaveInEditor\n\n// after\ninstance.hideFlags = HideFlags.None;\ninstance.transform.hideFlags = HideFlags.None;\nPrefabUtility.ReplacePrefabAssetOfPrefabInstance(instance, asset, mode);","handlingStrategy":"validation","validationCode":"if (prefabInstanceRoot.hideFlags.HasFlag(HideFlags.DontSaveInEditor)\n    || prefabInstanceRoot.transform.hideFlags.HasFlag(HideFlags.DontSaveInEditor))\n{\n    prefabInstanceRoot.hideFlags = HideFlags.None;\n    prefabInstanceRoot.transform.hideFlags = HideFlags.None;\n}\nPrefabUtility.ReplacePrefabAssetOfPrefabInstance(prefabInstanceRoot, prefabAssetRoot, mode);","typeGuard":"static bool HasNoDontSaveFlag(GameObject obj)\n{\n    return obj != null\n        && !obj.hideFlags.HasFlag(HideFlags.DontSaveInEditor)\n        && !obj.transform.hideFlags.HasFlag(HideFlags.DontSaveInEditor);\n}","tryCatchPattern":null,"preventionTips":["Avoid setting DontSaveInEditor on objects you intend to operate on with Prefab APIs.","Audit editor-only objects for stray hide flags before batch operations."],"tags":["prefab","hideflags","dont-save-in-editor","argument-validation","unity-editor"],"backgroundTag":null,"analyzedSha":"225b0fbdb57cc17d094e8056b71f8314aba56f73","analyzedAt":"2026-08-13T19:07:19.849Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}