Unity ECS : System does not find entities

1.1k Views Asked by At

I am having a problem with Unity ECS. My system does not find my entity with its query. This is my setup : I am currently testing with just one entity, that is created by a prefab, using this function :

Entity CreateEntity => GameObjectConversionUtility.ConvertGameObjectHierarchy(_parameters.carPrefab, _conversionSettings);

with

_assetStore = new BlobAssetStore();
_conversionSettings = GameObjectConversionSettings.FromWorld(World.DefaultGameObjectInjectionWorld, _assetStore);

My entity gets created properly (as i guess based on the entity window) : enter image description here

My system uses this query :

Entities.ForEach((DynamicBuffer<EdgeBufferElement> pathBuffer, ref Translation position,
                ref Rotation rotation, ref CarComponentData car) => {
     //logic
}).Schedule(inputDeps);

But my system logic does not get executed. This is propably because the system shows that it can not find the entity : enter image description here

I also tried to change the query to just reference the Translation but that lead to the same result. Do you have any idea what is going wrong here ?

Thanks for the help in advance !

1

There are 1 best solutions below

0
Andrew Łukasik On BEST ANSWER

This is because entities with Prefab tags are ignored in queries by default (you can easily override this ofc).

This promotes "prefab entity" pattern as calling EntityManager.Instantiate( prefab_entity ) is A LOT faster than EntityManager.Instantiate( prefab_gameObject )

enter image description here

using Unity.Entities;
using Unity.Jobs;
public class LetsTestQuerySystem : SystemBase
{
    protected override void OnCreate ()
    {
        Entity prefab = EntityManager.CreateEntity( typeof(Prefab) , typeof(MyComponent) );
        Entity instance = EntityManager.Instantiate( prefab );// Instantiate removes Prefab tag
        EntityManager.SetName( prefab , "prefab" );
        EntityManager.SetName( instance , "instance" );
    }
    protected override void OnUpdate ()
    {
        Entities
            // .WithAll<Prefab>()// query prefabs only
            .ForEach( ( ref MyComponent component ) => component.Value += 1 )
            .WithBurst().ScheduleParallel();
    }
    struct MyComponent : IComponentData { public int Value; }
}