{"record":{"id":"a8c00c88806b9b7b","repo":"Unity-Technologies/UnityCsReference","slug":"the-asset-gameobject-cannot-be-the-root-as-the-roo","errorCode":null,"errorMessage":"The asset GameObject cannot be the root as the root cannot be removed as an override.","messagePattern":"The asset GameObject cannot be the root as the root cannot be removed as an override\\.","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"Editor/Mono/Prefabs/PrefabUtility.cs","lineNumber":1570,"sourceCode":"            var removedGameObjects = PrefabUtility.GetRemovedGameObjects(prefabInstanceObject);\n#pragma warning disable UA2001 // The Banned API Analyzer produces compile errors for any new Linq code. This pre-existing usage has been suppressed, but should be rewritten if possible.\n            var filteredRemovedGameObjects = (from go in removedGameObjects where go != null select go).ToArray();\n#pragma warning restore UA2001\n            PrefabUtility.SetRemovedGameObjects(prefabInstanceObject, filteredRemovedGameObjects);\n        }\n\n        public static void ApplyRemovedGameObject(GameObject gameObjectInInstance, GameObject assetGameObject, InteractionMode action)\n        {\n            DateTime startTime = DateTime.UtcNow;\n\n            ThrowExceptionIfNotValidPrefabInstanceObject(gameObjectInInstance, true);\n\n            if (assetGameObject == null)\n                throw new ArgumentNullException(nameof(assetGameObject), \"Prefab source must not be null.\");\n            if (!IsPrefabInstanceObjectOf(gameObjectInInstance, PrefabUtility.GetPrefabAssetHandle(assetGameObject)))\n                throw new ArgumentException(\"Prefab instance must match Prefab source.\");\n            if (assetGameObject.transform.root == assetGameObject.transform)\n                throw new ArgumentException(\"The asset GameObject cannot be the root as the root cannot be removed as an override.\");\n\n            var actionName = \"Apply Prefab removed GameObject\";\n            var prefabInstanceObject = PrefabUtility.GetPrefabInstanceHandle(gameObjectInInstance);\n            GameObject prefabInstanceRoot = GetOutermostPrefabInstanceRoot(gameObjectInInstance);\n\n            if (prefabInstanceObject == null)\n                throw new ArgumentNullException(nameof(prefabInstanceObject), \"Prefab instance must not be null.\");\n\n            PrefabUtility.Internal_CallPrefabInstanceApplying(prefabInstanceRoot);\n\n            string assetPath = AssetDatabase.GetAssetPath(assetGameObject);\n            GameObject assetRoot = GetRootGameObject(assetGameObject);\n            byte[] originalFileContent = null;\n\n            if (action == InteractionMode.UserAction)\n            {\n                if (!FileUtil.ReadFileContentBinary(assetPath, out originalFileContent, out string errorMessage))\n                    Debug.LogError($\"No undo was registered when removing GameObject {assetGameObject.name} from {assetRoot.name}. \\nError: {errorMessage}\", assetRoot);","sourceCodeStart":1552,"sourceCodeEnd":1588,"githubUrl":"https://github.com/Unity-Technologies/UnityCsReference/blob/225b0fbdb57cc17d094e8056b71f8314aba56f73/Editor/Mono/Prefabs/PrefabUtility.cs#L1552-L1588","documentation":"Thrown by ApplyRemovedGameObject when assetGameObject.transform.root == assetGameObject.transform, meaning the supplied asset GameObject is the prefab root. The root can never be a 'removed child' override (the root always exists), so Unity rejects it with ArgumentException. Removing the root is not a valid override operation.","triggerScenarios":"Calling ApplyRemovedGameObject with assetGameObject set to the prefab asset's root GameObject; passing the outermost prefab root instead of a child that was actually removed.","commonSituations":"Tooling that iterates removed GameObjects and accidentally includes the root; passing GetOutermostPrefabInstanceRoot result where a child is required; misunderstanding that removed-child overrides exclude the root.","solutions":["Exclude the root: ensure assetGameObject.transform.root != assetGameObject.transform before calling.","Operate on a non-root child of the asset hierarchy that was actually removed from the instance.","If you need to remove the whole instance, use the appropriate instance-removal API, not a removed-child override."],"exampleFix":"// before\nPrefabUtility.ApplyRemovedGameObject(instanceGo, assetRootGo, InteractionMode.UserAction);\n\n// after\nif (assetGameObject.transform.root != assetGameObject.transform)\n    PrefabUtility.ApplyRemovedGameObject(instanceGo, assetGameObject, InteractionMode.UserAction);","handlingStrategy":"validation","validationCode":"if (assetGameObject.transform.root == assetGameObject.transform)\n{ Debug.LogWarning(\"Asset GameObject is the prefab root; cannot be a removed-child override.\"); return; }\nPrefabUtility.ApplyRemovedGameObject(gameObjectInInstance, assetGameObject, action);","typeGuard":"static bool IsNonRootChild(GameObject assetGo)\n    => assetGo != null && assetGo.transform.root != assetGo.transform;","tryCatchPattern":null,"preventionTips":["Never pass the prefab root as the assetGameObject for a removed-child override.","Filter out root GameObjects when iterating removed children.","Use instance-removal APIs to remove whole instances, not removed-child overrides."],"tags":["prefab","validation","root","removed-gameobject"],"backgroundTag":null,"analyzedSha":"225b0fbdb57cc17d094e8056b71f8314aba56f73","analyzedAt":"2026-08-13T19:07:19.849Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}