{"record":{"id":"d8e138800b6a0c6d","repo":"Unity-Technologies/UnityCsReference","slug":"prefabassetroot","errorCode":null,"errorMessage":"prefabAssetRoot","messagePattern":"prefabAssetRoot","errorType":"validation","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"Editor/Mono/Prefabs/PrefabUtility.cs","lineNumber":2243,"sourceCode":"                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));\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));","sourceCodeStart":2225,"sourceCodeEnd":2261,"githubUrl":"https://github.com/Unity-Technologies/UnityCsReference/blob/225b0fbdb57cc17d094e8056b71f8314aba56f73/Editor/Mono/Prefabs/PrefabUtility.cs#L2225-L2261","documentation":"Thrown by ThrowIfInvalidArgumentsForReplacePrefabInstance when prefabAssetRoot is null. This is the second null guard (after prefabInstanceRoot), ensuring both the instance and the replacement asset are non-null before proceeding to the detailed asset-validity checks.","triggerScenarios":"Calling ReplacePrefabInstance (internal) with prefabAssetRoot == null, usually from a failed asset lookup or an unassigned reference.","commonSituations":"AssetDatabase.LoadAssetAtPath returned null. A serialized Prefab reference was not assigned. The asset was renamed or moved between lookup and the replace call.","solutions":["Null-check prefabAssetRoot before calling the replace API.","Validate the asset path string and existence with AssetDatabase.LoadAssetAtPath before calling.","Re-resolve the asset after any AssetDatabase.Refresh."],"exampleFix":"// before\nGameObject assetRoot = AssetDatabase.LoadAssetAtPath<GameObject>(path);\nThrowIfInvalidArgumentsForReplacePrefabInstance(instanceRoot, assetRoot, true, mode);\n// after\nGameObject assetRoot = AssetDatabase.LoadAssetAtPath<GameObject>(path);\nif (assetRoot == null) { Debug.LogError(\"Missing asset: \" + path); return; }\nThrowIfInvalidArgumentsForReplacePrefabInstance(instanceRoot, assetRoot, true, mode);","handlingStrategy":"type-guard","validationCode":"if (prefabAssetRoot == null)\n{ Debug.LogError(\"prefabAssetRoot is null; verify the asset path.\"); return; }\nPrefabUtility.ThrowIfInvalidArgumentsForReplacePrefabInstance(instanceRoot, prefabAssetRoot, checkValidAsset, mode);","typeGuard":"static bool AreReplaceArgsValid(GameObject instance, GameObject asset) =>\n    instance != null && asset != null;","tryCatchPattern":null,"preventionTips":["Null-check both instance and asset root before calling replace.","Validate asset paths with LoadAssetAtPath before replace.","Re-resolve references after any AssetDatabase operation."],"tags":["unity","prefab","null-reference","replace-instance"],"backgroundTag":null,"analyzedSha":"225b0fbdb57cc17d094e8056b71f8314aba56f73","analyzedAt":"2026-08-13T19:07:19.849Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}