Unity-Technologies/UnityCsReference · error · InvalidOperationException
Cannot replace the Prefab instance when it has a missing scr
Error message
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. What it means
Thrown by ThrowIfInvalidArgumentsForReplacePrefabInstance when mode == InteractionMode.UserAction and FindGameObjectsWithInvalidComponent(prefabInstanceRoot) returns at least one GameObject with a missing script. Unity's Undo system cannot record operations on missing scripts, so a user-initiated replace is blocked. The message names the first offending GameObject and tells you to use InteractionMode.AutomatedAction to bypass the Undo constraint.
Source
Thrown at Editor/Mono/Prefabs/PrefabUtility.cs:2271
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)
throw new ArgumentNullException(nameof(prefabInstanceRoots));
if (prefabInstanceRoots.Length == 0)
throw new ArgumentException(nameof(prefabInstanceRoots) + " has no objects");
if (settings == null)
throw new ArgumentNullException(nameof(settings));View on GitHub (pinned to 225b0fbdb5)
Solutions
- Remove the missing script components from the Prefab instance before replacing (use GameObjectUtility.RemoveMonoScripts or manually remove in Inspector).
- Pass InteractionMode.AutomatedAction instead of UserAction if you don't need Undo support.
- Restore the missing MonoScript / fix compilation errors so the component resolves.
Example fix
// before PrefabUtility.ReplacePrefabAssetOfPrefabInstance(instance, asset, InteractionMode.UserAction); // Throws: instance has missing script // after — use AutomatedAction to bypass Undo limitation PrefabUtility.ReplacePrefabAssetOfPrefabInstance(instance, asset, InteractionMode.AutomatedAction);
Defensive patterns
Strategy: validation
Validate before calling
if (mode == InteractionMode.UserAction)
{
var invalid = PrefabUtility.FindGameObjectsWithInvalidComponent(prefabInstanceRoot);
if (invalid.Count > 0)
{
// Either remove missing scripts or switch to AutomatedAction
mode = InteractionMode.AutomatedAction;
}
}
PrefabUtility.ReplacePrefabAssetOfPrefabInstance(prefabInstanceRoot, prefabAssetRoot, mode); Type guard
static bool HasNoMissingScripts(GameObject root)
{
return PrefabUtility.FindGameObjectsWithInvalidComponent(root).Count == 0;
} Prevention
- Clean up missing script components after deleting MonoScripts.
- When building automated pipelines, prefer InteractionMode.AutomatedAction to avoid Undo-related blocks.
When it happens
Trigger: A Prefab instance in the scene has a component whose MonoScript was deleted or never compiled (shows as 'Missing (Mono Script)' in the Inspector). Calling ReplacePrefabAssetOfPrefabInstance with InteractionMode.UserAction on such an instance.
Common situations: After deleting a script that was used by Prefabs but not cleaning up the components. Upgrading a project where an assembly is missing and components became 'missing'. Importing assets from a collaborator who didn't include all scripts.
Related errors
- Cannot convert the GameObject when it has a missing script.
- Cannot replace the Prefab instance with the Prefab Asset '{A
- The Prefab you want to instantiate is null.
- ApplyPrefabAddedGameObjects requires that GameObjects share
- ApplyPrefabAddedGameObjects requires that GameObjects share
AI-assisted analysis of Unity-Technologies/UnityCsReference@225b0fbdb5 (2026-08-13).
Data as JSON: /api/errors/ab0842f1054d6ac3.
Report an issue: GitHub.