egametang/ET · error · Exception

cant set parent self: {this.GetType().FullName}

Error message

cant set parent self: {this.GetType().FullName}

What it means

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.

Source

Thrown at Packages/cn.etetet.core/Scripts/Core/Share/Entity/Entity.cs:200

        [AllowEntityMember]
        private Entity parent;

        // 可以改变parent,但是不能设置为null
        [MemoryPackIgnore]
        [BsonIgnore]
        public Entity Parent
        {
            get => this.parent;
            protected set
            {
                if (value == null)
                {
                    throw new Exception($"cant set parent null: {this.GetType().FullName}");
                }

                if (value == this)
                {
                    throw new Exception($"cant set parent self: {this.GetType().FullName}");
                }

                // 严格限制parent必须要有iSence,也就是说parent必须在数据树上面
                if (value.IScene == null)
                {
                    throw new Exception($"cant set parent because parent iSence is null: {this.GetType().FullName} {value.GetType().FullName}");
                }

                if (this.parent != null) // 之前有parent
                {
                    // parent相同,不设置
                    if (this.parent == value)
                    {
                        Log.Error($"重复设置了Parent: {this.GetType().FullName} parent: {this.parent.GetType().FullName}");
                        return;
                    }

                    this.parent.RemoveChild(this.Id, false);

View on GitHub (pinned to 5cab01f7a8)

Solutions

  1. Skip the assignment when the candidate parent is the entity itself.
  2. In bulk reparent routines, exclude the destination node from the source set.
  3. Add an early `if (value == this) return;` guard in your own reparent helper.

Example fix

// before
foreach (var child in group.Children)
{
    child.Parent = newParent;   // newParent may equal child
}

// after
foreach (var child in group.Children)
{
    if (child == newParent) continue;
    child.Parent = newParent;
}
Defensive patterns

Strategy: validation

Validate before calling

if (newParent != null && newParent != entity)
{
    entity.Parent = newParent;
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of egametang/ET@5cab01f7a8 (2026-08-13). Data as JSON: /api/errors/267a72562a707faa. Report an issue: GitHub.