0

I'm trying to do the following:

private void AssignPlayer()
{
    EntityQuery playerQuery = GetEntityQuery(ComponentType.ReadOnly<PlayerTag>());
    Entity playerEntity = playerQuery.ToEntityArray(Allocator.Temp);

    Entities.
        WithAll<ChaserTag>().
        ForEach((ref TargetData targetData) =>
        {
            if (playerEntity != Entity.Null)
            {
                targetData.targetEntity = playerEntity;
            }

        }).Schedule();
}

But the line that assigns targetData.targetEntity = playerEntity; throws the error:

Cannot implicitly convert type 'Unity.Collections.NativeArray<Unity.Entities.Entity>' to 'Unity.Entities.Entity'

2
  • I'm trying to assign one follower to one leader Jun 25, 2020 at 6:34
  • NativeArray<Entity> playerEntities = playerQuery.ToEntityArray(Allocator.TempJob); Entity playerEntity = playerEntities[0]; playerEntities.Dispose(); Jun 25, 2020 at 14:20

1 Answer 1

1
if( playerQuery.CalculateEntityCount()!=0 )
{
    NativeArray<Entity> players = playerQuery.ToEntityArray( Allocator.TempJob );
    Entities.WithName("targetData_update_job")
        .WithReadOnly( players ).WithDeallocateOnJobCompletion( players )
        .WithAll<ChaserTag>()
        .ForEach( ( ref TargetData targetData ) =>
        {
            targetData.targetEntity = players[0];
        }
        ).ScheduleParallel();
}
else Debug.LogWarning($"{nameof(AssignPlayer)}(): no players tho");

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.