{"record":{"id":"d014c1c246586062","repo":"Unity-Technologies/UnityCsReference","slug":"parameter-prefabassetgameobject-is-null","errorCode":null,"errorMessage":"Parameter prefabAssetGameObject is null","messagePattern":"Parameter prefabAssetGameObject is null","errorType":"exception","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"Editor/Mono/Prefabs/PrefabUtility.cs","lineNumber":2018,"sourceCode":"            }\n\n            var empty = new GameObject(\"EmptyPrefab\");\n            bool success;\n            var assetObject = SaveAsPrefabAsset(empty, path, out success);\n            Object.DestroyImmediate(empty);\n            return PrefabUtility.GetPrefabObject(assetObject);\n        }\n\n        public static GameObject SavePrefabAsset(GameObject asset)\n        {\n            bool savedSuccesfully;\n            return SavePrefabAsset(asset, out savedSuccesfully);\n        }\n\n        public static GameObject SavePrefabAsset(GameObject asset, out bool savedSuccessfully)\n        {\n            if (asset == null)\n                throw new ArgumentNullException(\"Parameter prefabAssetGameObject is null\");\n\n            // Include model check even though models are also immutable, since we can give a more clear exception message.\n            if (IsPartOfModelPrefab(asset))\n                throw new ArgumentException(\"Can't save a Model Prefab\");\n\n            if (IsPartOfImmutablePrefab(asset))\n                throw new ArgumentException(\"Can't save an immutable Prefab\");\n\n            string path = AssetDatabase.GetAssetPath(asset);\n            if (String.IsNullOrEmpty(path))\n                throw new ArgumentException(\"Can't save a Prefab instance\");\n\n            var root = asset.transform.root.gameObject;\n            if (root != asset)\n                throw new ArgumentException(\"GameObject to save Prefab from must be a Prefab root\");\n\n            return SavePrefabAsset_Internal(root, out savedSuccessfully);\n        }","sourceCodeStart":2000,"sourceCodeEnd":2036,"githubUrl":"https://github.com/Unity-Technologies/UnityCsReference/blob/225b0fbdb57cc17d094e8056b71f8314aba56f73/Editor/Mono/Prefabs/PrefabUtility.cs#L2000-L2036","documentation":"SavePrefabAsset requires a non-null asset GameObject. The method dereferences the asset immediately (type checks, path lookup), so a null argument throws ArgumentNullException. Note the message refers to 'prefabAssetGameObject' (the internal parameter name) rather than the public 'asset' name.","triggerScenarios":"Calling PrefabUtility.SavePrefabAsset(null) or SavePrefabAsset(null, out _); passing a reference that resolved to null.","commonSituations":"Resolving the prefab asset via a query that returned null (e.g., selection active object was not a prefab); field never initialized before the save call.","solutions":["Null-check the asset before calling.","Resolve the asset from a known source (AssetDatabase.LoadAssetAtPath) and verify before saving.","Guard with a clear editor message when the resolved asset is null."],"exampleFix":"// before\nPrefabUtility.SavePrefabAsset(asset);\n\n// after\nif (asset != null)\n    PrefabUtility.SavePrefabAsset(asset);","handlingStrategy":"validation","validationCode":"if (asset == null) return;","typeGuard":"static bool IsPrefabAsset(GameObject asset) => asset != null;","tryCatchPattern":null,"preventionTips":["Null-check the asset before saving.","Resolve the asset from a known source and verify before calling SavePrefabAsset.","Show a clear editor message when the resolved asset is null."],"tags":["prefab","unity","null-argument","validation"],"backgroundTag":null,"analyzedSha":"225b0fbdb57cc17d094e8056b71f8314aba56f73","analyzedAt":"2026-08-13T19:07:19.849Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}