{"record":{"id":"9253bf7a801fbe67","repo":"Unity-Technologies/UnityCsReference","slug":"cannot-replace-the-prefab-instance-with-the-prefab","errorCode":null,"errorMessage":"Cannot replace the Prefab instance with the Prefab Asset '{AssetDatabase.GetAssetPath(prefabAsset)}' because it has a missing script. GameObject '{gameObjectsWithInvalidScript[0].name}' in the Prefab Asset has a missing script.","messagePattern":"Cannot replace the Prefab instance with the Prefab Asset '(.+?)' because it has a missing script\\. GameObject '(.+?)' in the Prefab Asset has a missing script\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"Editor/Mono/Prefabs/PrefabUtility.cs","lineNumber":2234,"sourceCode":"            return InstantiatePrefab_internal(assetComponentOrGameObject, EditorSceneManager.GetTargetSceneForNewGameObjects(), parent);\n        }\n\n        internal static void ThrowIfInvalidAssetForReplacePrefabInstance(GameObject prefabAsset, InteractionMode action)\n        {\n            if (prefabAsset == null)\n                throw new ArgumentNullException(nameof(prefabAsset));\n\n            if (!EditorUtility.IsPersistent(prefabAsset))\n                throw new ArgumentException(\"Input Prefab asset is not an asset object. Input asset: \" + prefabAsset.name, nameof(prefabAsset));\n\n            var assetPath = AssetDatabase.GetAssetPath(prefabAsset);\n            if (assetPath.StartsWith(\"Library/\"))\n                throw new InvalidOperationException(string.Format(\"Cannot replace the Prefab instance since the Prefab Asset is invalid for instance replacement. Prefab Asset path: \" + assetPath));\n\n            // Recording undo does not handle missing scripts\n            var gameObjectsWithInvalidScript = FindGameObjectsWithInvalidComponent(prefabAsset);\n            if (action == InteractionMode.UserAction && gameObjectsWithInvalidScript.Count > 0)\n                throw new InvalidOperationException(string.Format($\"Cannot replace the Prefab instance with the Prefab Asset '{AssetDatabase.GetAssetPath(prefabAsset)}' because it has a missing script. GameObject '{gameObjectsWithInvalidScript[0].name}' in the Prefab Asset has a missing script.\"));\n        }\n\n        internal static void ThrowIfInvalidArgumentsForReplacePrefabInstance(GameObject prefabInstanceRoot, GameObject prefabAssetRoot, bool checkValidAsset, InteractionMode mode)\n        {\n            if (prefabInstanceRoot == null)\n                throw new ArgumentNullException(nameof(prefabInstanceRoot));\n\n            if (prefabAssetRoot == null)\n                throw new ArgumentNullException(nameof(prefabAssetRoot));\n\n            if (checkValidAsset)\n                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));","sourceCodeStart":2216,"sourceCodeEnd":2252,"githubUrl":"https://github.com/Unity-Technologies/UnityCsReference/blob/225b0fbdb57cc17d094e8056b71f8314aba56f73/Editor/Mono/Prefabs/PrefabUtility.cs#L2216-L2252","documentation":"Thrown by ThrowIfInvalidAssetForReplacePrefabInstance in UserAction mode when the Prefab asset contains one or more GameObjects with missing scripts (MonoBehaviour components whose script class cannot be resolved). Undo recording cannot handle missing scripts, so the replace is blocked during a user action to prevent corruption.","triggerScenarios":"Calling ReplacePrefabInstance with InteractionMode.UserAction where FindGameObjectsWithInvalidComponent(prefabAsset) returns a non-empty list. The missing-script check is skipped for non-UserAction modes (automated action).","commonSituations":"A script was deleted or renamed without updating the Prefab asset. Script GUID changed after a package update or namespace rename. Prefab imported from another project referencing scripts not present.","solutions":["Fix the missing scripts in the Prefab asset: reassign the correct MonoBehaviour or remove the broken component.","Use InteractionMode.AutomatedAction if you intentionally accept missing scripts (note: undo will not be recorded).","Audit the asset with FindGameObjectsWithInvalidComponent before attempting replace."],"exampleFix":"// before\nThrowIfInvalidAssetForReplacePrefabInstance(asset, InteractionMode.UserAction);\n// after\nvar bad = PrefabUtility.FindGameObjectsWithInvalidComponent(asset);\nif (bad.Count > 0)\n    Debug.LogError(\"Fix missing scripts on: \" + bad[0].name);\nelse\n    ThrowIfInvalidAssetForReplacePrefabInstance(asset, InteractionMode.UserAction);","handlingStrategy":"validation","validationCode":"var invalid = PrefabUtility.FindGameObjectsWithInvalidComponent(prefabAsset);\nif (invalid.Count > 0)\n{ Debug.LogError(\"Prefab has missing scripts: \" + invalid[0].name); return; }\nPrefabUtility.ThrowIfInvalidAssetForReplacePrefabInstance(prefabAsset, InteractionMode.UserAction);","typeGuard":"static bool HasNoMissingScripts(GameObject asset)\n{\n    if (asset == null) return false;\n    return PrefabUtility.FindGameObjectsWithInvalidComponent(asset).Count == 0;\n}","tryCatchPattern":"try { PrefabUtility.ThrowIfInvalidAssetForReplacePrefabInstance(asset, InteractionMode.UserAction); }\ncatch (InvalidOperationException e) when (e.Message.Contains(\"missing script\"))\n{ Debug.LogError(\"Fix missing scripts before replace.\"); }","preventionTips":["Fix or remove missing scripts in Prefab assets before replace.","Use AutomatedAction only if you accept missing-script risk (no undo).","Audit Prefabs with FindGameObjectsWithInvalidComponent in CI."],"tags":["unity","prefab","missing-script","replace-instance","undo"],"backgroundTag":null,"analyzedSha":"225b0fbdb57cc17d094e8056b71f8314aba56f73","analyzedAt":"2026-08-13T19:07:19.849Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}