{"record":{"id":"b08411f3b79e8695","repo":"Unity-Technologies/UnityCsReference","slug":"gameobject-to-save-prefab-from-must-be-a-prefab-ro","errorCode":null,"errorMessage":"GameObject to save Prefab from must be a Prefab root","messagePattern":"GameObject to save Prefab from must be a Prefab root","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"Editor/Mono/Prefabs/PrefabUtility.cs","lineNumber":2033,"sourceCode":"        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        }\n\n        internal static void ValidatePath(GameObject instanceRoot, string path)\n        {\n            if (String.IsNullOrEmpty(path))\n                throw new ArgumentNullException(\"path is null or empty\");\n\n            if (!Paths.IsValidAssetPath(path, \".prefab\"))\n                throw new ArgumentException(\"Given path is not valid: '\" + path + \"'\");\n\n            if (Directory.Exists(path))\n                throw new ArgumentException(\"Overwriting a folder with an Asset is not allowed: '\" + path + \"'\");\n\n            string directory = Path.GetDirectoryName(path);\n\n            // We allow relative paths outside the Assets folder so we do not throw if isValidAssetFolder is false","sourceCodeStart":2015,"sourceCodeEnd":2051,"githubUrl":"https://github.com/Unity-Technologies/UnityCsReference/blob/225b0fbdb57cc17d094e8056b71f8314aba56f73/Editor/Mono/Prefabs/PrefabUtility.cs#L2015-L2051","documentation":"SavePrefabAsset operates on whole prefabs, so the supplied asset must be the root of its prefab. If asset.transform.root.gameObject != asset, the caller passed a non-root child and ArgumentException is thrown. The method then forwards the root to the internal save routine.","triggerScenarios":"Passing a child GameObject of a prefab asset rather than the prefab root.","commonSituations":"Selecting a child object within a prefab asset and calling save; selection logic that hands back a sub-object instead of the root.","solutions":["Resolve the root before saving: asset = asset.transform.root.gameObject.","Filter selection to root objects, or walk up to the root explicitly.","Verify asset.transform.root.gameObject == asset before calling."],"exampleFix":"// before\nPrefabUtility.SavePrefabAsset(childGo);\n\n// after\nPrefabUtility.SavePrefabAsset(childGo.transform.root.gameObject);","handlingStrategy":"validation","validationCode":"GameObject root = asset.transform.root.gameObject;\nif (root != asset) asset = root;","typeGuard":"static bool IsPrefabRoot(GameObject asset) =>\n    asset != null && asset.transform.root.gameObject == asset;","tryCatchPattern":null,"preventionTips":["Resolve asset.transform.root.gameObject before saving.","Filter selection to root objects, or walk up to the root explicitly.","Verify the supplied object equals its transform root before calling."],"tags":["prefab","unity","root","hierarchy"],"backgroundTag":null,"analyzedSha":"225b0fbdb57cc17d094e8056b71f8314aba56f73","analyzedAt":"2026-08-13T19:07:19.849Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}