Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question A component with type:{0} has not been added to the entity.

Discussion in 'Entity Component System' started by Cell-i-Zenit, Nov 13, 2020.

  1. Cell-i-Zenit

    Cell-i-Zenit

    Joined:
    Mar 11, 2016
    Posts:
    290
    Hi,

    ArgumentException: System.ArgumentException: A component with type:{0} has not been added to the entity.

    I think i have a brain fart.. not sure what iam doing wrong:

    Code (CSharp):
    1. protected override void OnUpdate()
    2.     {
    3.       var commandBuffer = _commandBufferSystem.CreateCommandBuffer();
    4.       var entityArchetype = _checkpoint;
    5.       var archetype = _finish;
    6.  
    7.       var handle = Entities.ForEach((Entity entity, in SpawnFlag spawnFlag) =>
    8.         {
    9.           if (spawnFlag.ObjectType == ObjectType.Checkpoint)
    10.           {
    11.             var newEntity = commandBuffer.CreateEntity(entityArchetype);
    12.             commandBuffer.SetComponent(newEntity, new Translation() {Value = spawnFlag.Position});
    13.           }
    14.           else if (spawnFlag.ObjectType == ObjectType.Finish)
    15.           {
    16.             var newEntity = commandBuffer.CreateEntity(archetype);
    17.             commandBuffer.SetComponent(newEntity, new Translation() {Value = spawnFlag.Position});
    18.           }
    19.  
    20.           commandBuffer.DestroyEntity(entity);
    21.         })
    22.         .Schedule(Dependency);
    23.  
    24.       _commandBufferSystem.AddJobHandleForProducer(handle);
    25.  
    26.       Dependency = handle;
    27.  
    28.     }
    Code (CSharp):
    1. ArgumentException: System.ArgumentException: A component with type:{0} has not been added to the entity.
    2. Thrown from job: Unity.Entities.EntityCommandBuffer._mono_to_burst_PlaybackChainChunk
    3. This Exception was thrown from a function compiled with Burst, which has limited exception support. Turn off burst (Jobs -> Burst -> Enable Compilation) to inspect full exceptions & stacktraces.
    4. EntityCommandBuffer was recorded in Core.Systems.InitializationGroup.SpawnerSystem and played back in Unity.Entities.EndInitializationEntityCommandBufferSystem.
    5.   at (wrapper managed-to-native) System.Object.wrapper_native_0000025E7F6F01E0(intptr,intptr,Unity.Entities.EntityComponentStore/ArchetypeChanges&,Unity.Entities.ECBSharedPlaybackState&,intptr,int,int,bool,Unity.Entities.PlaybackPolicy)
    6.   at (wrapper delegate-invoke)
     
  2. DK_A5B

    DK_A5B

    Joined:
    Jan 21, 2016
    Posts:
    110
    The problem is that when you're calling SetComponent, the Component of that type (in this case Translation) doesn't already exist on the Entity. You could solve this by:
    • Updating your EntityArchtype to include Translation
    • Changing SetComponent to AddComponent

    The better fix is almost certainly to add the Translation component to your EntityArchetype. Changing SetComponent to AddComponent would work, but it means you're changing the Archetype of your Entity immediately after creating it, which is wasteful because the ECS framework will have to copy the Component data out of the old Archetype and into the new Archetype.