Search Unity

Exception: Component has not been added to the entity Help

Discussion in 'Entity Component System' started by coldasfrost979, Jul 18, 2018.

  1. coldasfrost979

    coldasfrost979

    Joined:
    Jun 9, 2018
    Posts:
    25
    Hello,

    My code here:
    Code (CSharp):
    1. public class ResourceMouseOverSystem : ComponentSystem
    2. {
    3.     public struct MouseOverResourceTargetGroup
    4.     {
    5.         public ComponentDataArray<ResourceTypeTag> tag;
    6.         [ReadOnly] public ComponentArray<CapsuleCollider> Collider; //why can't I just use this? when I try, collider is always null
    7.         public EntityArray Entities;
    8.         public int Length;
    9.     }
    10.  
    11.     [Inject] MouseOverResourceTargetGroup resourceGroup;
    12.     protected override void OnUpdate()
    13.     {
    14.         var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    15.         for (int j = 0; j < resourceGroup.Length; j++)
    16.         {
    17.             var entity = resourceGroup.Entities[j];
    18.            
    19.             RaycastHit hit;
    20.             var collider = resourceGroup.Collider[j];
    21.            
    22.             if (collider.Raycast(ray, out hit, Mathf.Infinity))
    23.             {
    24.                 Debug.Log("I am hovering over it");
    25.                 if (!FallenBootstrap.entityManager.HasComponent(entity, ComponentType.Create<MouseOver>()))
    26.                 {
    27.                     PostUpdateCommands.SetComponent(entity, default(MouseOver));
    28.                     //FallenBootstrap.entityManager.AddComponent(entity, ComponentType.Create<MouseOver>());
    29.                 }
    30.             }
    31.             else
    32.             {
    33.                 if (FallenBootstrap.entityManager.HasComponent(entity, ComponentType.Create<MouseOver>()))
    34.                 {
    35.                     Debug.Log("I am removing a component");
    36.                     PostUpdateCommands.RemoveComponent<MouseOver>(entity);
    37.                 }
    38.             }
    39.         }
    40.     }
    41. }

    produces this when I mouse over the resource spherecollider:
    upload_2018-7-17_20-22-5.png


    any thoughts as to why? thanks!
     
  2. LazyGameDevZA

    LazyGameDevZA

    Joined:
    Nov 10, 2016
    Posts:
    143
    On line 27 you are using PostUpdateCommands.SetComponent. Try changing this to PostUpdateCommands.AddComponent. The former is trying to set the component value and if the entity doesn't have it attached it will throw this exception.