egametang/ET · error · Exception

entity already has component: {type.FullName}

Error message

entity already has component: {type.FullName}

What it means

Thrown by Entity.CreateComponent when the entity already contains a component of the given type. The ECS allows at most one component per concrete type per entity; adding a duplicate type is rejected because the lookup map already holds its LongHashCode.

Source

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

            return component;
        }

        private static Entity Create(Type type, bool isFromPool)
        {
            Entity component = (Entity) ObjectPool.Fetch(type, isFromPool);
            component.IsFromPool = true;
            component.IsNoDeserializeSystem = true;
            component.Id = 0;
            return component;
        }

        protected Entity CreateComponent(Type type, long id, bool isFromPool)
        {
            this.CheckThread();
            
            if (this.components != null && this.components.ContainsKey(this.GetComponentLongHashCode(type)))
            {
                throw new Exception($"entity already has component: {type.FullName}");
            }

            Entity component = Create(type, isFromPool);
            component.Id = id;
            component.ComponentParent = this;
            return component;
        }

        protected Entity CreateChild(Type type, long id, bool isFromPool)
        {
            this.CheckThread();
            
            Entity component = Create(type, isFromPool);
            component.Id = id;
            component.Parent = this;
            return component;
        }

View on GitHub (pinned to 5cab01f7a8)

Solutions

  1. Before adding, check GetComponent(type) and RemoveComponent first if a fresh instance is desired.
  2. Make init/idempotent so AddComponent for that type runs only once.
  3. Use GetComponentOrNull to test existence rather than assuming absence.

Example fix

// before
entity.AddComponent(typeof(MyComp), fromPool);

// after
if (entity.GetComponentOrNull(typeof(MyComp)) == null)
{
    entity.AddComponent(typeof(MyComp), fromPool);
}
Defensive patterns

Strategy: validation

Validate before calling

if (entity.GetComponentOrNull(type) == null)
{
    entity.AddComponent(type, isFromPool);
}

Prevention

When it happens

Trigger: Calling CreateComponent for a type already in components; AddComponent path that internally calls CreateComponent for an existing type; deserialization adding a component that already exists on the entity.

Common situations: Calling AddComponent twice for the same type without removing first; InitSystems/AwakeSystems running twice; reload/restart logic re-initializing an entity.

Related errors


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