egametang/ET · error · Exception

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

Error message

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

What it means

Thrown by the Entity.Parent setter when the new parent is null. The ECS forbids detaching an entity into a null parent — every entity must live in the scene tree. To remove an entity, call Dispose()/Remove(), not `entity.Parent = null`.

Source

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

        [MemoryPackIgnore]
        [BsonIgnore]
        public bool IsDisposed => this.InstanceId == 0;
        
        [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)
                    {

View on GitHub (pinned to 5cab01f7a8)

Solutions

  1. To remove an entity from its parent, call Dispose() or parent.RemoveChild/RemoveComponent — never assign Parent=null.
  2. Null-check the candidate parent before assigning and bail out or create it in a scene first.
  3. Ensure the target parent entity is itself attached to a scene before reparenting onto it.

Example fix

// before
entity.Parent = foundParent;   // foundParent may be null

// after
if (foundParent == null)
{
    Log.Error("reparent target missing, keeping current parent");
    return;
}
entity.Parent = foundParent;
Defensive patterns

Strategy: validation

Validate before calling

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

Prevention

When it happens

Trigger: Assigning `entity.Parent = null`; reparenting logic that falls back to null on a missing target; a deserialization path that receives a null parent reference; code copying a parent variable that turned out null.

Common situations: Mistaking Detach semantics (you expected Parent=null to detach); parent lookup returns null because the target entity was already disposed or not added to a scene; refactoring that moved the parent assignment into a helper that can be passed null.

Related errors


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