{"record":{"id":"b24188e847b83253","repo":"Unity-Technologies/UnityCsReference","slug":"input-0-is-not-a-prefab-instance-for-plain-ga","errorCode":null,"errorMessage":"Input '{0}' is not a Prefab instance, for plain GameObjects use ConvertToPrefabInstance() instead","messagePattern":"Input '(.+?)' is not a Prefab instance, for plain GameObjects use ConvertToPrefabInstance\\(\\) instead","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"Editor/Mono/Prefabs/PrefabUtility.cs","lineNumber":2249,"sourceCode":"            // 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));\n            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            {","sourceCodeStart":2231,"sourceCodeEnd":2267,"githubUrl":"https://github.com/Unity-Technologies/UnityCsReference/blob/225b0fbdb57cc17d094e8056b71f8314aba56f73/Editor/Mono/Prefabs/PrefabUtility.cs#L2231-L2267","documentation":"Thrown by ThrowIfInvalidArgumentsForReplacePrefabInstance when the GameObject passed as prefabInstanceRoot is not part of a Prefab instance in a scene (IsPartOfNonAssetPrefabInstance returns false). ReplacePrefabAssetOfPrefabInstance expects an existing Prefab instance to swap its source asset; a plain scene GameObject is not eligible. The message points you to ConvertToPrefabInstance() which is the correct API for turning a plain GameObject into a Prefab instance.","triggerScenarios":"Calling PrefabUtility.ReplacePrefabAssetOfPrefabInstance(plainGameObject, prefabAsset, mode) where plainGameObject was never instantiated from a Prefab (e.g. created via new GameObject()). Also happens if the object was previously unpacked with UnpackPrefabInstance and is now a plain scene object.","commonSituations":"Script that generically applies a 'swap prefab' operation to a selection that mixes plain GameObjects and Prefab instances. Editor automation that assumes a dragged object is always a Prefab instance. Migrating from the legacy ReplacePrefab API where plain GameObjects were sometimes accepted.","solutions":["If the object is a plain GameObject, call PrefabUtility.ConvertToPrefabInstance() instead.","Branch on PrefabUtility.IsPartOfNonAssetPrefabInstance(obj) before choosing which API to call.","Ensure the object you pass was instantiated from a Prefab asset (e.g. via Instantiate on a loaded Prefab, not new GameObject)."],"exampleFix":"// before\nPrefabUtility.ReplacePrefabAssetOfPrefabInstance(myGo, prefabAsset, InteractionMode.AutomatedAction);\n\n// after\nif (PrefabUtility.IsPartOfNonAssetPrefabInstance(myGo))\n    PrefabUtility.ReplacePrefabAssetOfPrefabInstance(myGo, prefabAsset, InteractionMode.AutomatedAction);\nelse\n    PrefabUtility.ConvertToPrefabInstance(myGo, prefabAsset, new ConvertToPrefabInstanceSettings(), InteractionMode.AutomatedAction);","handlingStrategy":"validation","validationCode":"// Validate before calling ReplacePrefabAssetOfPrefabInstance\nif (!PrefabUtility.IsPartOfNonAssetPrefabInstance(prefabInstanceRoot))\n{\n    // Use ConvertToPrefabInstance instead, or skip\n    PrefabUtility.ConvertToPrefabInstance(prefabInstanceRoot, prefabAssetRoot, new ConvertToPrefabInstanceSettings(), mode);\n    return;\n}\nPrefabUtility.ReplacePrefabAssetOfPrefabInstance(prefabInstanceRoot, prefabAssetRoot, mode);","typeGuard":"static bool IsReplaceablePrefabInstance(GameObject obj)\n{\n    return obj != null\n        && PrefabUtility.IsPartOfNonAssetPrefabInstance(obj)\n        && !EditorUtility.IsPersistent(obj);\n}","tryCatchPattern":null,"preventionTips":["Always branch on PrefabUtility.IsPartOfNonAssetPrefabInstance before choosing Replace vs Convert API.","In bulk operations, separate plain GameObjects from Prefab instances into two lists and call the appropriate API for each."],"tags":["prefab","argument-validation","unity-editor","gameobject"],"backgroundTag":null,"analyzedSha":"225b0fbdb57cc17d094e8056b71f8314aba56f73","analyzedAt":"2026-08-13T19:07:19.849Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}