Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Selecting script not working when containing ForEach

Discussion in 'Entity Component System' started by Holyren, Jul 11, 2020.

  1. Holyren

    Holyren

    Joined:
    Jul 8, 2017
    Posts:
    35
    For a 3d game, I am trying to create a select system based on raycast from camera to mouse position. If it returns an Entity, then a tag is added to the entity. However to remove old tags, I run a foreach loop to remove them, however it stops the script from functioning with no errors. I do find it weird as I have seen multiple similar examples of such scripts.

    As a side note, does anyone have a recommendation on how to create such a system in a better way

    Code (CSharp):
    1.     protected override void OnUpdate()
    2.     {
    3.         if (Input.GetKeyDown(KeyCode.Mouse0))
    4.         {          
    5.             float3 cameraPosition = MonoToEntityManager.instance.CameraPosition();
    6.             Camera cameraComponent = MonoToEntityManager.instance.cameraComponent;
    7.  
    8.             float3 rayDirection = cameraComponent.ScreenPointToRay(Input.mousePosition).direction;
    9.  
    10.             Entity rayHitEntity = Raycast(cameraPosition, rayDirection * 50f);
    11.  
    12.             if (rayHitEntity == Entity.Null)
    13.             {
    14.                 Debug.DrawRay(cameraPosition, rayDirection * 50f, Color.red, 10f);
    15.                 return;
    16.             }
    17.  
    18.             Entities.WithAll<Tag_SelectedEntity>().ForEach((Entity entity) =>
    19.             {
    20.                 EntityManager.RemoveComponent<Tag_SelectedEntity>(entity);
    21.             }).WithoutBurst().Run();
    22.  
    23.             EntityManager.AddComponent<Tag_SelectedEntity>(rayHitEntity);
    24.  
    25.             Debug.DrawRay(cameraPosition, rayDirection * 50f, Color.green, 10f);
    26.         }
    27.     }
     
  2. Cell-i-Zenit

    Cell-i-Zenit

    Joined:
    Mar 11, 2016
    Posts:
    288
    Why not store the latest "selected" entity and just remove it the moment you hit something different? This way you dont need to have such a foreach loop
     
  3. Holyren

    Holyren

    Joined:
    Jul 8, 2017
    Posts:
    35
    That's true, this is one way to fix it. I am however unsure why my code still does not work. Do you have any ideas?
     
  4. Holyren

    Holyren

    Joined:
    Jul 8, 2017
    Posts:
    35
    The whole script seems to be working when an Entity with the correct tag exists. However when removed, the whole script stops working. Is there a way around this?
     
  5. DreamingImLatios

    DreamingImLatios

    Joined:
    Jun 3, 2017
    Posts:
    3,984
    Instead of WithoutBurst(), use WithStructuralChanges().
     
  6. eizenhorn

    eizenhorn

    Joined:
    Oct 17, 2016
    Posts:
    2,653
    [AlwaysUpdateSystem] on system.
    ILPP will interpret your code as a different thing which also will create EntityQuery for the system, which in turn decide - should run system or not. If you haven't entities which match EntityQuery - the system wouldn't run, this is how it works by design.
     
  7. Holyren

    Holyren

    Joined:
    Jul 8, 2017
    Posts:
    35
    Thanks, now it is working.
     
  8. DanSuperGP

    DanSuperGP

    Joined:
    Apr 7, 2013
    Posts:
    408
    Hidden wizardry...

    How are you supposed to know that? Is it in the documentation somewhere?
     
  9. yondercode

    yondercode

    Joined:
    Jun 11, 2018
    Posts:
    27