{"record":{"id":"c092eaa7c7b6cb60","repo":"Unity-Technologies/UnityCsReference","slug":"value-cannot-be-null-parameter-prefabasset","errorCode":null,"errorMessage":"Value cannot be null. (Parameter 'prefabAsset')","messagePattern":"Value cannot be null\\. \\(Parameter 'prefabAsset'\\)","errorType":"exception","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"Editor/Mono/Prefabs/PrefabUtility.cs","lineNumber":2222,"sourceCode":"        {\n            return InstantiatePrefab_internal(assetComponentOrGameObject, EditorSceneManager.GetTargetSceneForNewGameObjects(), null);\n        }\n\n        // Instantiates the given prefab in a given scene\n        public static Object InstantiatePrefab(Object assetComponentOrGameObject, Scene destinationScene)\n        {\n            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));","sourceCodeStart":2204,"sourceCodeEnd":2240,"githubUrl":"https://github.com/Unity-Technologies/UnityCsReference/blob/225b0fbdb57cc17d094e8056b71f8314aba56f73/Editor/Mono/Prefabs/PrefabUtility.cs#L2204-L2240","documentation":"Thrown by ThrowIfInvalidAssetForReplacePrefabInstance when prefabAsset is null. This guard ensures a valid Prefab asset is provided before attempting to replace a Prefab instance with it.","triggerScenarios":"Calling ReplacePrefabInstance (internal) with a null prefabAsset argument, typically from a failed AssetDatabase lookup or unassigned reference.","commonSituations":"AssetDatabase.LoadAssetAtPath returned null due to wrong path or missing asset. Serialized Prefab reference lost after domain reload. Editor script assumes an asset exists that was renamed or deleted.","solutions":["Null-check prefabAsset before calling the replace API.","Verify the asset path with AssetDatabase.LoadAssetAtPath and check for null before proceeding.","Re-resolve asset references after AssetDatabase.Refresh or domain reload."],"exampleFix":"// before\nGameObject asset = AssetDatabase.LoadAssetAtPath<GameObject>(badPath);\nThrowIfInvalidAssetForReplacePrefabInstance(asset, mode);\n// after\nGameObject asset = AssetDatabase.LoadAssetAtPath<GameObject>(path);\nif (asset == null) { Debug.LogError(\"Asset not found: \" + path); return; }\nThrowIfInvalidAssetForReplacePrefabInstance(asset, mode);","handlingStrategy":"type-guard","validationCode":"if (prefabAsset == null)\n{ Debug.LogError(\"Prefab asset is null; check the asset path.\"); return; }\nPrefabUtility.ThrowIfInvalidAssetForReplacePrefabInstance(prefabAsset, mode);","typeGuard":"static bool IsValidReplacementAsset(GameObject asset) =>\n    asset != null && EditorUtility.IsPersistent(asset);","tryCatchPattern":null,"preventionTips":["Validate asset lookups return non-null before replace operations.","Log asset paths that fail to load for easier debugging.","Re-resolve asset references after AssetDatabase.Refresh."],"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"}