{"record":{"id":"2c650112e5f6d387","repo":"Unity-Technologies/UnityCsReference","slug":"input-instance-root-is-from-a-prefab-asset-this-i","errorCode":null,"errorMessage":"Input instance root is from a Prefab asset, this is not supported. Input instance: ","messagePattern":"Input instance root is from a Prefab asset, this is not supported\\. Input instance: ","errorType":"validation","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"Editor/Mono/Prefabs/PrefabUtility.cs","lineNumber":2254,"sourceCode":"\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            {\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            }","sourceCodeStart":2236,"sourceCodeEnd":2272,"githubUrl":"https://github.com/Unity-Technologies/UnityCsReference/blob/225b0fbdb57cc17d094e8056b71f8314aba56f73/Editor/Mono/Prefabs/PrefabUtility.cs#L2236-L2272","documentation":"Thrown by ThrowIfInvalidArgumentsForReplacePrefabInstance when EditorUtility.IsPersistent(prefabInstanceRoot) is true, meaning the object lives in the Asset Database (a .prefab file on disk) rather than being a live instance in a scene. ReplacePrefabAssetOfPrefabInstance operates on scene-level instances only; you cannot feed it a Prefab asset's root GameObject.","triggerScenarios":"Loading a Prefab asset via AssetDatabase.LoadAssetAtPath or Resources.Load and passing that asset root directly as the 'instance' argument. Using AssetDatabase.FindAssets to locate Prefabs and then calling replace on the asset objects.","commonSituations":"Confusion between Prefab assets (stored in the project) and Prefab instances (live in a scene). Code that processes a folder of .prefab files and tries to 'replace' them in-place.","solutions":["Instantiate the Prefab asset into a scene first, then pass the instantiated root to ReplacePrefabAssetOfPrefabInstance.","If you need to modify a Prefab asset directly, use PrefabUtility.LoadPrefabContents / SaveAsPrefabAsset instead.","Add a guard: if (EditorUtility.IsPersistent(obj)) return; before the call."],"exampleFix":"// before\nvar assetRoot = AssetDatabase.LoadAssetAtPath<GameObject>(\"Assets/My.prefab\");\nPrefabUtility.ReplacePrefabAssetOfPrefabInstance(assetRoot, newAsset, mode);\n\n// after\nvar instance = (GameObject)PrefabUtility.InstantiatePrefab(assetRoot);\nPrefabUtility.ReplacePrefabAssetOfPrefabInstance(instance, newAsset, mode);","handlingStrategy":"validation","validationCode":"if (EditorUtility.IsPersistent(prefabInstanceRoot))\n{\n    Debug.LogError(\"Cannot use a Prefab asset as instance root; instantiate it first.\");\n    return;\n}\nPrefabUtility.ReplacePrefabAssetOfPrefabInstance(prefabInstanceRoot, prefabAssetRoot, mode);","typeGuard":"static bool IsSceneLevelInstance(GameObject obj)\n{\n    return obj != null && !EditorUtility.IsPersistent(obj);\n}","tryCatchPattern":null,"preventionTips":["Distinguish between assets loaded from disk (persistent) and live scene instances in your data model.","Instantiate assets into a scene before operating on them with instance-level APIs."],"tags":["prefab","argument-validation","persistent-asset","unity-editor"],"backgroundTag":null,"analyzedSha":"225b0fbdb57cc17d094e8056b71f8314aba56f73","analyzedAt":"2026-08-13T19:07:19.849Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}