{"record":{"id":"3f2f970948a6e0b4","repo":"Unity-Technologies/UnityCsReference","slug":"cyclic-nesting-detected","errorCode":null,"errorMessage":"Cyclic nesting detected","messagePattern":"Cyclic nesting detected","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"Editor/Mono/Prefabs/PrefabUtility.bindings.cs","lineNumber":326,"sourceCode":"                {\n                    if (parentPrefabInstance != targetPrefabInstance)\n                    {\n                        throw new ArgumentException(\"GameObjects must be parented under the same Prefab instance.\");\n                    }\n                }\n\n                if (PrefabUtility.IsPartOfNonAssetPrefabInstance(go))\n                {\n                    var correspondingGO = PrefabUtility.GetCorrespondingObjectFromSource(go);\n                    var correspondingGOPrefabObject = PrefabUtility.GetPrefabAssetHandle(correspondingGO);\n                    if (targetPrefabObject == correspondingGOPrefabObject)\n                        throw new ArgumentException(\"GameObject is already part of target prefab\");\n                }\n            }\n\n            string prefabGUID = AssetDatabase.AssetPathToGUID(AssetDatabase.GetAssetPath(targetPrefab));\n            if (!VerifyNestingFromScript(gameObjects, prefabGUID, null))\n                throw new ArgumentException(\"Cyclic nesting detected\");\n\n            AddGameObjectsToPrefabAndConnect_Internal(gameObjects, targetPrefab);\n        }\n\n        [NativeMethod(\"AddGameObjectsToPrefabAndConnect\", IsFreeFunction = true)]\n        extern private static void AddGameObjectsToPrefabAndConnect_Internal([NotNull] GameObject[] gameObjects, [NotNull] Object prefab);\n\n        [NativeMethod(\"VerifyNestingFromScript\", IsFreeFunction = true)]\n        extern private static bool VerifyNestingFromScript([NotNull] GameObject[] gameObjects, [NotNull] string targetPrefabGUID, Object prefabInstance);\n\n        [StaticAccessor(\"PrefabUtilityBindings\", StaticAccessorType.DoubleColon)]\n        [NativeMethod(ThrowsException = true)]\n        extern internal static Component[] GetRemovedComponents([NotNull] Object prefabInstance);\n\n        [StaticAccessor(\"PrefabUtilityBindings\", StaticAccessorType.DoubleColon)]\n        [NativeMethod(ThrowsException = true)]\n        extern static void SetRemovedComponents([NotNull] Object prefabInstance, [NotNull] Component[] removedComponents);\n","sourceCodeStart":308,"sourceCodeEnd":344,"githubUrl":"https://github.com/Unity-Technologies/UnityCsReference/blob/225b0fbdb57cc17d094e8056b71f8314aba56f73/Editor/Mono/Prefabs/PrefabUtility.bindings.cs#L308-L344","documentation":"Thrown when VerifyNestingFromScript returns false, indicating that connecting the GameObjects into the target prefab would create a cyclic prefab nesting (a prefab eventually containing itself). Unity performs a native graph check before the connect call to prevent infinite recursion in the prefab hierarchy.","triggerScenarios":"Nesting prefab A inside prefab B when B (or its variant) is already nested under A, forming a cycle. Variants and nested prefabs that, through overrides applied to the target, would reference back to an ancestor.","commonSituations":"Building recursive/recursive-looking prefab structures. Connecting a child whose corresponding prefab is an ancestor of the target in the nesting graph. Restructuring prefabs without checking the existing nesting chain.","solutions":["Break the cycle: ensure the target prefab is not already nested under any prefab referenced by the objects being connected.","Audit the nesting graph of target and inputs before calling.","Use a separate, non-cyclic prefab as the target, or flatten the conflicting nesting first."],"exampleFix":"// before\n// A nests B, B nests A-equivalent -> cyclic\nPrefabUtility.AddGameObjectsToPrefabAndConnect(gos, targetA);\n\n// after\n// VerifyNestingFromScript mirrors the native check; if it returns false, abort.\n// Restructure so target is not an ancestor of the inputs' source prefabs.\nif (!VerifyNoCycle(gos, target)) { Debug.LogError(\"Would create a cycle.\"); return; }\nPrefabUtility.AddGameObjectsToPrefabAndConnect(gos, target);","handlingStrategy":"validation","validationCode":"// Mirror the native guard: build the nesting graph and reject cycles before calling.\n// Unity does not expose VerifyNestingFromScript publicly; track ancestor GUIDs manually.\nif (WouldCreateCycle(gameObjects, targetPrefab))\n{ Debug.LogError(\"Connecting would create a cyclic prefab nesting.\"); return; }","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Maintain an acyclic prefab-nesting graph; never nest a prefab under one of its own descendants.","Before connecting, walk the target's ancestors and reject if any input source prefab appears.","Document nesting relationships in the project to prevent accidental cycles."],"tags":["prefab","add-to-prefab","cyclic","nesting"],"backgroundTag":null,"analyzedSha":"225b0fbdb57cc17d094e8056b71f8314aba56f73","analyzedAt":"2026-08-13T19:07:19.849Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}