{"record":{"id":"ab0842f1054d6ac3","repo":"Unity-Technologies/UnityCsReference","slug":"cannot-replace-the-prefab-instance-when-it-has-a-m","errorCode":null,"errorMessage":"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.","messagePattern":"Cannot replace the Prefab instance when it has a missing script\\. GameObject '(.+?)' has a missing script\\. Use InteractionMode\\.AutomatedAction to force the replace\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"Editor/Mono/Prefabs/PrefabUtility.cs","lineNumber":2271,"sourceCode":"            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)\n                throw new ArgumentNullException(nameof(prefabInstanceRoots));\n\n            if (prefabInstanceRoots.Length == 0)\n                throw new ArgumentException(nameof(prefabInstanceRoots) + \" has no objects\");\n\n            if (settings == null)\n                throw new ArgumentNullException(nameof(settings));","sourceCodeStart":2253,"sourceCodeEnd":2289,"githubUrl":"https://github.com/Unity-Technologies/UnityCsReference/blob/225b0fbdb57cc17d094e8056b71f8314aba56f73/Editor/Mono/Prefabs/PrefabUtility.cs#L2253-L2289","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before\nPrefabUtility.ReplacePrefabAssetOfPrefabInstance(instance, asset, InteractionMode.UserAction);\n// Throws: instance has missing script\n\n// after — use AutomatedAction to bypass Undo limitation\nPrefabUtility.ReplacePrefabAssetOfPrefabInstance(instance, asset, InteractionMode.AutomatedAction);","handlingStrategy":"validation","validationCode":"if (mode == InteractionMode.UserAction)\n{\n    var invalid = PrefabUtility.FindGameObjectsWithInvalidComponent(prefabInstanceRoot);\n    if (invalid.Count > 0)\n    {\n        // Either remove missing scripts or switch to AutomatedAction\n        mode = InteractionMode.AutomatedAction;\n    }\n}\nPrefabUtility.ReplacePrefabAssetOfPrefabInstance(prefabInstanceRoot, prefabAssetRoot, mode);","typeGuard":"static bool HasNoMissingScripts(GameObject root)\n{\n    return PrefabUtility.FindGameObjectsWithInvalidComponent(root).Count == 0;\n}","tryCatchPattern":null,"preventionTips":["Clean up missing script components after deleting MonoScripts.","When building automated pipelines, prefer InteractionMode.AutomatedAction to avoid Undo-related blocks."],"tags":["prefab","missing-script","undo","interaction-mode","unity-editor"],"backgroundTag":null,"analyzedSha":"225b0fbdb57cc17d094e8056b71f8314aba56f73","analyzedAt":"2026-08-13T19:07:19.849Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}