Unity-Technologies/UnityCsReference · error · InvalidOperationException

Cannot convert the GameObject when it has a missing script.

Error message

Cannot convert the GameObject when it has a missing script. GameObject '{gameObjectsWithInvalidScript[0].name}' has a missing script. This is not supported by the Undo system. Use InteractionMode.AutomatedAction instead.

What it means

Thrown by ThrowIfInvalidArgumentsForConvertToPrefabInstance when mode == InteractionMode.UserAction and FindGameObjectsWithInvalidComponent(plainGameObject) finds at least one GameObject with a missing script. Unity's Undo system cannot record changes involving missing scripts, so a user-action conversion is blocked. The message names the offending GameObject and advises using InteractionMode.AutomatedAction to bypass Undo.

Source

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

            if (checkValidAsset)
                ThrowIfInvalidAssetForReplacePrefabInstance(prefabAssetRoot, mode);

            if (plainGameObject.transform.GetType() != prefabAssetRoot.transform.GetType())
                throw new InvalidOperationException(string.Format("Cannot convert the GameObject '{0}' with root transform of type {1} with a Prefab asset with root transform of type {2}. Transform types must match.", plainGameObject.name, plainGameObject.transform.GetType().Name, prefabAssetRoot.transform.GetType().Name));

            // Prefab Mode and EditPrefabContents scope handling
            if (PrefabStageUtility.IsGameObjectThePrefabRootInAnyPrefabStage(plainGameObject) || (EditorSceneManager.IsPreviewSceneObject(plainGameObject) && plainGameObject.transform.parent == null))
                throw new InvalidOperationException("Replacing the root GameObject in a Prefab with a Prefab instance is not supported since it will break all overrides for existing instances of this Prefab, including their positions and rotations." + plainGameObject.name);

            if (plainGameObject.hideFlags.HasFlag(HideFlags.DontSaveInEditor) || plainGameObject.transform.hideFlags.HasFlag(HideFlags.DontSaveInEditor))
                throw new ArgumentException("Input GameObject is using the HideFlags.DontSaveInEditor flag which is not supported when converting to Prefab instance: GameObject: " + plainGameObject.name, nameof(plainGameObject));

            if (mode == InteractionMode.UserAction)
            {
                // Recording undo does not handle missing scripts
                var gameObjectsWithInvalidScript = FindGameObjectsWithInvalidComponent(plainGameObject);
                if (gameObjectsWithInvalidScript.Count > 0)
                    throw new InvalidOperationException(string.Format($"Cannot convert the GameObject when it has a missing script. GameObject '{gameObjectsWithInvalidScript[0].name}' has a missing script. This is not supported by the Undo system. Use InteractionMode.AutomatedAction instead."));
            }
        }

        public static void ConvertToPrefabInstance(GameObject plainGameObject, GameObject prefabAssetRoot, ConvertToPrefabInstanceSettings settings, InteractionMode mode)
        {
            ThrowIfInvalidArgumentsForConvertToPrefabInstance(plainGameObject, prefabAssetRoot, true, mode);

            ConvertToPrefabInstance_NoInputValidation(plainGameObject, prefabAssetRoot, settings, mode);

            EditorUtility.ForceRebuildInspectors();
        }

        public static void ConvertToPrefabInstances(GameObject[] plainGameObjects, GameObject prefabAssetRoot, ConvertToPrefabInstanceSettings settings, InteractionMode mode)
        {
            if (plainGameObjects == null)
                throw new ArgumentNullException(nameof(plainGameObjects));

            if (plainGameObjects.Length == 0)

View on GitHub (pinned to 225b0fbdb5)

Solutions

  1. Remove the missing script components before converting (Inspector right-click → Remove Component, or GameObjectUtility.RemoveMonoScripts).
  2. Pass InteractionMode.AutomatedAction instead of UserAction if Undo support is not needed.
  3. Fix compilation errors / restore the missing MonoScript so the component resolves.

Example fix

// before
PrefabUtility.ConvertToPrefabInstance(plainGo, prefabAsset, settings, InteractionMode.UserAction);
// Throws: plainGo (or a child) has a missing script

// after — use AutomatedAction to bypass the Undo limitation
PrefabUtility.ConvertToPrefabInstance(plainGo, prefabAsset, settings, InteractionMode.AutomatedAction);
Defensive patterns

Strategy: validation

Validate before calling

if (mode == InteractionMode.UserAction)
{
    var invalid = PrefabUtility.FindGameObjectsWithInvalidComponent(plainGameObject);
    if (invalid.Count > 0)
        mode = InteractionMode.AutomatedAction;
}
PrefabUtility.ConvertToPrefabInstance(plainGameObject, prefabAssetRoot, settings, mode);

Type guard

static bool HasNoMissingScripts(GameObject root)
{
    return PrefabUtility.FindGameObjectsWithInvalidComponent(root).Count == 0;
}

Prevention

When it happens

Trigger: A plain GameObject (or one of its children) has a 'Missing (Mono Script)' component. Calling ConvertToPrefabInstance with InteractionMode.UserAction on such an object.

Common situations: After deleting a script used by scene objects without removing the components. Project upgrade where an assembly is missing. Importing scene data from a collaborator missing scripts.

Related errors


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