Unity-Technologies/UnityCsReference · error · ArgumentException

Input instance root is using the HideFlags.DontSaveInEditor…

Error message

Input instance root is using the HideFlags.DontSaveInEditor flag which is not supported when replacing: Input instance: 

What it means

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.

Solutions

  1. Remove HideFlags.DontSaveInEditor from the instance root (and its transform) before calling ReplacePrefabAssetOfPrefabInstance.
  2. Operate on a normal scene instance that is serialized normally.
  3. Guard: if (obj.hideFlags.HasFlag(HideFlags.DontSaveInEditor)) return;

Example fix

// before
PrefabUtility.ReplacePrefabAssetOfPrefabInstance(instance, asset, mode);
// instance has HideFlags.DontSaveInEditor

// after
instance.hideFlags = HideFlags.None;
instance.transform.hideFlags = HideFlags.None;
PrefabUtility.ReplacePrefabAssetOfPrefabInstance(instance, asset, mode);
Defensive patterns

Strategy: validation

Validate before calling

if (prefabInstanceRoot.hideFlags.HasFlag(HideFlags.DontSaveInEditor)
    || prefabInstanceRoot.transform.hideFlags.HasFlag(HideFlags.DontSaveInEditor))
{
    prefabInstanceRoot.hideFlags = HideFlags.None;
    prefabInstanceRoot.transform.hideFlags = HideFlags.None;
}
PrefabUtility.ReplacePrefabAssetOfPrefabInstance(prefabInstanceRoot, prefabAssetRoot, mode);

Type guard

static bool HasNoDontSaveFlag(GameObject obj)
{
    return obj != null
        && !obj.hideFlags.HasFlag(HideFlags.DontSaveInEditor)
        && !obj.transform.hideFlags.HasFlag(HideFlags.DontSaveInEditor);
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of Unity-Technologies/UnityCsReference@225b0fbdb5 (2026-08-13). Data as JSON: /api/errors/2c2aa1ac6098879f. Report an issue: GitHub.

Appendix: source

Thrown at Editor/Mono/Prefabs/PrefabUtility.cs:2264

                ThrowIfInvalidAssetForReplacePrefabInstance(prefabAssetRoot, mode);

            if (!IsPartOfNonAssetPrefabInstance(prefabInstanceRoot))
                throw new InvalidOperationException(string.Format("Input '{0}' is not a Prefab instance, for plain GameObjects use ConvertToPrefabInstance() instead", prefabInstanceRoot.name));

            if (!IsOutermostPrefabInstanceRoot(prefabInstanceRoot))
                throw new ArgumentException("Input instance is not an outermost Prefab instance root. Input instance: " + prefabInstanceRoot.name, nameof(prefabInstanceRoot));
            if (EditorUtility.IsPersistent(prefabInstanceRoot))
                throw new ArgumentException("Input instance root is from a Prefab asset, this is not supported. Input instance: " + prefabInstanceRoot.name, nameof(prefabInstanceRoot));

            if (PrefabStageUtility.IsGameObjectThePrefabRootInAnyPrefabStage(prefabInstanceRoot))
                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);
            if (IsAnyPrefabInstanceRoot(prefabInstanceRoot) && EditorSceneManager.IsPreviewSceneObject(prefabInstanceRoot) && prefabInstanceRoot.transform.parent == null) // EditPrefabContentsScope handling
                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);
            if (prefabInstanceRoot.transform.GetType() != prefabAssetRoot.transform.GetType())
                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));

            if (prefabInstanceRoot.hideFlags.HasFlag(HideFlags.DontSaveInEditor) || prefabInstanceRoot.transform.hideFlags.HasFlag(HideFlags.DontSaveInEditor))
                throw new ArgumentException("Input instance root is using the HideFlags.DontSaveInEditor flag which is not supported when replacing: Input instance: " + prefabInstanceRoot.name, nameof(prefabInstanceRoot));

            if (mode == InteractionMode.UserAction)
            {
                // Recording undo does not handle missing scripts
                var gameObjectsWithInvalidScript = FindGameObjectsWithInvalidComponent(prefabInstanceRoot);
                if (gameObjectsWithInvalidScript.Count > 0)
                    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."));
            }
        }

        public static void ReplacePrefabAssetOfPrefabInstances(GameObject[] prefabInstanceRoots, GameObject prefabAssetRoot, InteractionMode mode)
        {
            ReplacePrefabAssetOfPrefabInstances(prefabInstanceRoots, prefabAssetRoot, new PrefabReplacingSettings(), mode);
        }

        public static void ReplacePrefabAssetOfPrefabInstances(GameObject[] prefabInstanceRoots, GameObject prefabAssetRoot, PrefabReplacingSettings settings, InteractionMode mode)
        {
            if (prefabInstanceRoots == null)

View on GitHub (pinned to 225b0fbdb5)