{"record":{"id":"267a72562a707faa","repo":"egametang/ET","slug":"cant-set-parent-self-this-gettype-fullname","errorCode":null,"errorMessage":"cant set parent self: {this.GetType().FullName}","messagePattern":"cant set parent self: (.+?)","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"Packages/cn.etetet.core/Scripts/Core/Share/Entity/Entity.cs","lineNumber":200,"sourceCode":"        [AllowEntityMember]\n        private Entity parent;\n\n        // 可以改变parent，但是不能设置为null\n        [MemoryPackIgnore]\n        [BsonIgnore]\n        public Entity Parent\n        {\n            get => this.parent;\n            protected set\n            {\n                if (value == null)\n                {\n                    throw new Exception($\"cant set parent null: {this.GetType().FullName}\");\n                }\n\n                if (value == this)\n                {\n                    throw new Exception($\"cant set parent self: {this.GetType().FullName}\");\n                }\n\n                // 严格限制parent必须要有iSence,也就是说parent必须在数据树上面\n                if (value.IScene == null)\n                {\n                    throw new Exception($\"cant set parent because parent iSence is null: {this.GetType().FullName} {value.GetType().FullName}\");\n                }\n\n                if (this.parent != null) // 之前有parent\n                {\n                    // parent相同，不设置\n                    if (this.parent == value)\n                    {\n                        Log.Error($\"重复设置了Parent: {this.GetType().FullName} parent: {this.parent.GetType().FullName}\");\n                        return;\n                    }\n\n                    this.parent.RemoveChild(this.Id, false);","sourceCodeStart":182,"sourceCodeEnd":218,"githubUrl":"https://github.com/egametang/ET/blob/5cab01f7a8bee5f49f4781eebe9e2b1c6d7ebe0f/Packages/cn.etetet.core/Scripts/Core/Share/Entity/Entity.cs#L182-L218","documentation":"Thrown by the Entity.Parent setter when the assigned value is the entity itself (value == this). The ECS forbids an entity being its own parent, which would create a cycle in the tree.","triggerScenarios":"Assigning `entity.Parent = entity`; reparenting a list where the loop variable accidentally equals the entity; a generic 'reparent all children onto X' routine where X is one of the children.","commonSituations":"Bulk reparenting over a collection that includes the destination; UI/hierarchy tooling that re-parents a node onto itself; copy-paste of reparent code without excluding self.","solutions":["Skip the assignment when the candidate parent is the entity itself.","In bulk reparent routines, exclude the destination node from the source set.","Add an early `if (value == this) return;` guard in your own reparent helper."],"exampleFix":"// before\nforeach (var child in group.Children)\n{\n    child.Parent = newParent;   // newParent may equal child\n}\n\n// after\nforeach (var child in group.Children)\n{\n    if (child == newParent) continue;\n    child.Parent = newParent;\n}","handlingStrategy":"validation","validationCode":"if (newParent != null && newParent != entity)\n{\n    entity.Parent = newParent;\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Exclude the destination node from bulk reparent sets.","Add `value == this` guards in custom reparent helpers.","Validate identity before assigning Parent."],"tags":["entity","ecs","parent","self-cycle","core"],"backgroundTag":null,"analyzedSha":"5cab01f7a8bee5f49f4781eebe9e2b1c6d7ebe0f","analyzedAt":"2026-08-13T21:10:40.377Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}