Search Unity

  1. Unity 2019.1 beta is now available.
    Dismiss Notice
  2. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  3. We're looking for insight from anyone who has experience with game testing to help us better Unity. Take our survey here. If chosen to participate you'll be entered into a sweepstake to win an Amazon gift card.
    Dismiss Notice
  4. On February 28th the Feedback website will shut down and be redirected to the Unity forums. See the full post for more information.
    Dismiss Notice
  5. Want to provide direct feedback to the Unity team? Join the Unity Advisory Panel.
    Dismiss Notice
  6. Unity 2018.3 is now released.
    Dismiss Notice
  7. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice

Job to have entity follow position of another entity causes InvalidOperationException

Discussion in 'Entity Component System and C# Job system' started by andoco, Feb 11, 2019.

  1. andoco


    Feb 24, 2014
    I have a Follow component and system to allow the position of one entity to be set to the position of an entity being followed:

    Code (CSharp):
    1. public struct Follow : IComponentData
    2. {
    3.     public Entity target;
    4. }
    6. public class FollowSystem : JobComponentSystem
    7. {
    8.     struct FollowJob : IJobProcessComponentDataWithEntity<Follow, Position>
    9.     {
    10.         [ReadOnly] public ComponentDataFromEntity<Position> Position;
    12.         public void Execute(Entity entity, int index, [ReadOnly] ref Follow follow, ref Position position)
    13.         {
    14.             if ( != Entity.Null && Position.Exists(
    15.             {
    16.                 var targetPos = Position[];
    17.                 position.Value = targetPos.Value;
    18.             }
    19.         }
    20.     }
    22.     protected override JobHandle OnUpdate(JobHandle inputDeps)
    23.     {
    24.         var job = new FollowJob
    25.         {
    26.             Position = GetComponentDataFromEntity<Position>()
    27.         };
    29.         return job.Schedule(this, inputDeps);
    30.     }
    31. }
    But when I attempt to run this with one entity set to follow another, I get the exception:

    What is the cause of this exception, and is it possible to keep the following entity's position set to the target's in this way?

    As a workaround I'm using an EndFrameBarrier command buffer instead:

    Code (CSharp):
    1. public class FollowSystem : JobComponentSystem
    2. {
    3.     struct FollowJob : IJobProcessComponentDataWithEntity<Follow>
    4.     {
    5.         public EntityCommandBuffer.Concurrent Ecb;
    6.         [ReadOnly] public ComponentDataFromEntity<Position> Position;
    8.         public void Execute(Entity entity, int index, [ReadOnly] ref Follow follow)
    9.         {
    10.             if ( != Entity.Null && Position.Exists(
    11.             {
    12.                 var targetPos = Position[];
    13.                 Ecb.SetComponent(index, entity, targetPos);
    14.             }
    15.         }
    16.     }
    18.     [Inject] private EndFrameBarrier endFrameBarrier;
    20.     protected override JobHandle OnUpdate(JobHandle inputDeps)
    21.     {
    22.         var job = new FollowJob
    23.         {
    24.             Ecb = endFrameBarrier.CreateCommandBuffer().ToConcurrent(),
    25.             Position = GetComponentDataFromEntity<Position>()
    26.         };
    28.         return job.Schedule(this, inputDeps);
    29.     }
    30. }
    Is this a better approach than trying to set the Position component value inside the job?

  2. Micz84


    Jul 21, 2012
    The cause of this issue is in [ReadOnly] permission for Position component array. You have to add ReadOnly attribute for ComponentDataFromEntity, but then you can't write to position array.
    Beside your solution with ECB you could split it to two jobs and create temporary hashmap. In the first job populate HashMap with entity->targetPos pairs. In the second job read from this hash map and write to position component, but I do not know if it is a better solution then yours.
  3. GilCat


    Sep 21, 2013
    You can't have a job executing on Position and have a field ComponentDataFromEntity<Position> because they will result in the same array (aliasing).
    You can already to that following with the Attach component but if you for some reason want to do this follow system you should read from the LocalToWorld and not the Position.

    Code (CSharp):
    1.   struct FollowJob : IJobProcessComponentDataWithEntity<Follow, Position> {
    2.     [ReadOnly]
    3.     public ComponentDataFromEntity<LocalToWorld> LocalToWorldFromEntity;
    5.     public void Execute(Entity entity, int index, [ReadOnly] ref Follow follow, [WriteOnly]ref Position position) {
    6.       if ( != Entity.Null && LocalToWorldFromEntity.Exists( {
    7.         var targetPos = LocalToWorldFromEntity[];
    8.         position.Value = targetPos;
    9.       }
    10.     }
    11.   }
  4. andoco


    Feb 24, 2014
    Using Attach sounds like the best approach in general but it doesn't seem to work when trying to attach a GameObjectEntity (on the scene Camera in this case) to some other pure ECS entity.

    Thanks for the other explanations on why the aliasing is occurring. It makes more sense now.
  5. tertle


    Jan 25, 2011
    Should work fine, you just need the one of the hybrid link components, either CopyTransfromFromGameObject or CopyTransformToGameObject to sync them.


    actually from memory CopyTransformToGameObjectSystem does not take into account parents so this will probably not work if your child is an GameObject.
    Last edited: Feb 14, 2019
  6. GilCat


    Sep 21, 2013
    I also use that a lot to attach Unity UI elements (Monobehaviours) to pure ECS elements.
    That will do the trick.