{"record":{"id":"9e9381957bf1eb0a","repo":"Unity-Technologies/UnityCsReference","slug":"object-is-a-parentless-root","errorCode":null,"errorMessage":"Object is a parentless root.","messagePattern":"Object is a parentless root\\.","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"Editor/Mono/Prefabs/PrefabUtility.cs","lineNumber":1740,"sourceCode":"                    action,\n                    startTime,\n                    false\n                );\n            }\n\n            PrefabUtility.Internal_CallPrefabInstanceApplied(instanceRoot);\n            EditorUtility.ForceRebuildInspectors();\n        }\n\n        internal static bool HasSameParent(GameObject[] gameObjects)\n        {\n            if (gameObjects == null || gameObjects.Length == 0 || gameObjects[0] == null)\n                throw new ArgumentException(nameof(gameObjects), \"Array is invalid.\");\n\n            Transform goParent = gameObjects[0].transform.parent;\n\n            if (goParent == null)\n                throw new ArgumentException(nameof(goParent), \"Object is a parentless root.\");\n\n            foreach (GameObject go in gameObjects)\n            {\n                if (go.transform.parent != goParent)\n                    return false;\n            }\n\n            return true;\n        }\n\n        private static void RemoveRemovedGameObjectOverride(Object instanceObject, GameObject assetGameObject)\n        {\n            var removedGameObjects = PrefabUtility.GetRemovedGameObjects(instanceObject);\n            int index = -1;\n            for (int i = 0; i < removedGameObjects.Length; i++)\n            {\n                //Walk the inheritance chain as you can give the base of a variant as the source asset\n                if (IsPrefabInstanceObjectOf(removedGameObjects[i], assetGameObject))","sourceCodeStart":1722,"sourceCodeEnd":1758,"githubUrl":"https://github.com/Unity-Technologies/UnityCsReference/blob/225b0fbdb57cc17d094e8056b71f8314aba56f73/Editor/Mono/Prefabs/PrefabUtility.cs#L1722-L1758","documentation":"HasSameParent throws when the first element has no parent transform — i.e. it is a root GameObject. The helper needs non-root objects to obtain a baseline parent for comparison, and added overrides are by definition not roots, so a root in this position is invalid.","triggerScenarios":"The first element of the array is a scene root or prefab instance root with transform.parent == null.","commonSituations":"Including the prefab instance root or a top-level scene object in a batch intended for added-override operations; selection logic that starts from a root.","solutions":["Exclude root objects from the array; added overrides are never roots, so filter g.transform.parent != null.","Verify g.transform.parent != null before adding any object to the apply batch.","Start selection collection from children of the instance root, not the root itself."],"exampleFix":"// before\nvar batch = selection.ToArray();\nPrefabUtility.ApplyAddedGameObjects(batch, path, mode);\n\n// after\nvar batch = selection.Where(g => g != null && g.transform.parent != null).ToArray();\nif (batch.Length > 0)\n    PrefabUtility.ApplyAddedGameObjects(batch, path, mode);","handlingStrategy":"validation","validationCode":"if (gameObjects == null || gameObjects.Length == 0) return false;\nif (gameObjects[0] == null || gameObjects[0].transform.parent == null) return false;","typeGuard":"static bool FirstElementHasParent(GameObject[] gos) =>\n    gos != null && gos.Length > 0 && gos[0] != null\n    && gos[0].transform.parent != null;","tryCatchPattern":null,"preventionTips":["Exclude root objects from apply batches; added overrides are never roots.","Start selection from children of the instance root, never the root itself.","Verify g.transform.parent != null for every candidate before batching."],"tags":["prefab","unity","hierarchy","validation"],"backgroundTag":null,"analyzedSha":"225b0fbdb57cc17d094e8056b71f8314aba56f73","analyzedAt":"2026-08-13T19:07:19.849Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}