{"record":{"id":"cffa56b96290e7da","repo":"Unity-Technologies/UnityCsReference","slug":"cannot-replace-the-prefab-instance-since-the-prefa","errorCode":null,"errorMessage":"Cannot replace the Prefab instance since the Prefab Asset is invalid for instance replacement. Prefab Asset path: {assetPath}","messagePattern":"Cannot replace the Prefab instance since the Prefab Asset is invalid for instance replacement\\. Prefab Asset path: (.+?)","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"Editor/Mono/Prefabs/PrefabUtility.cs","lineNumber":2229,"sourceCode":"            return InstantiatePrefab_internal(assetComponentOrGameObject, destinationScene, null);\n        }\n\n        public static Object InstantiatePrefab(Object assetComponentOrGameObject, Transform parent)\n        {\n            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","sourceCodeStart":2211,"sourceCodeEnd":2247,"githubUrl":"https://github.com/Unity-Technologies/UnityCsReference/blob/225b0fbdb57cc17d094e8056b71f8314aba56f73/Editor/Mono/Prefabs/PrefabUtility.cs#L2211-L2247","documentation":"Thrown by ThrowIfInvalidAssetForReplacePrefabInstance when the Prefab asset's path starts with \"Library/\". Assets under Library/ are generated/cached by Unity (not user-authored) and are not valid as replacement sources for a Prefab instance.","triggerScenarios":"The prefabAsset resolves to an asset whose AssetDatabase.GetAssetPath returns a Library/-prefixed path (e.g. a built-in default resource or a generated cache asset).","commonSituations":"Passing a built-in Unity resource (stored under Library/) as a replacement asset. Using an asset from a transient import cache. The asset path changed to Library/ after a reimport or cache migration.","solutions":["Use a user-authored Prefab asset stored under Assets/ as the replacement source.","Check AssetDatabase.GetAssetPath(asset).StartsWith(\"Library/\") before calling and reject such assets.","If you need a built-in as a template, copy it into Assets/ first via AssetDatabase.CopyAsset."],"exampleFix":"// before\nThrowIfInvalidAssetForReplacePrefabInstance(libraryAsset, mode);\n// after\nstring srcPath = AssetDatabase.GetAssetPath(libraryAsset);\nstring dstPath = \"Assets/MyCopy.prefab\";\nAssetDatabase.CopyAsset(srcPath, dstPath);\nGameObject goodAsset = AssetDatabase.LoadAssetAtPath<GameObject>(dstPath);\nThrowIfInvalidAssetForReplacePrefabInstance(goodAsset, mode);","handlingStrategy":"validation","validationCode":"string assetPath = AssetDatabase.GetAssetPath(prefabAsset);\nif (assetPath.StartsWith(\"Library/\"))\n{ Debug.LogError(\"Cannot use Library/ asset for replace: \" + assetPath); return; }","typeGuard":"static bool IsUserAuthoredAsset(GameObject asset)\n{\n    if (asset == null) return false;\n    string p = AssetDatabase.GetAssetPath(asset);\n    return !p.StartsWith(\"Library/\");\n}","tryCatchPattern":null,"preventionTips":["Only pass user-authored Assets/ Prefabs to replace operations.","Check the asset path prefix before calling replace.","Copy built-in/Library resources into Assets/ if a template is needed."],"tags":["unity","prefab","library-path","replace-instance"],"backgroundTag":null,"analyzedSha":"225b0fbdb57cc17d094e8056b71f8314aba56f73","analyzedAt":"2026-08-13T19:07:19.849Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}