So I’m making an RTS with ECS, and like every other RTS, you can move units by setting a target position where the unit should go.
The idea is to have a Unit Entity and a Target Visualizer Entity for each Unit, the problem lies on the creation of this visualizer, i have a prefab with the visualizer graphics and i want to instantiate it on unit creation. Something like this:
[UpdateInGroup(typeof(InitializationSystemGroup))]
public partial struct TargetInitializationSystem : ISystem
{
public Entity targetPrefab;
public EntityQuery _query;
[BurstCompile]
public void OnCreate(ref SystemState state)
{
_query = new EntityQueryBuilder(Allocator.Temp)
.WithDisabled<TargetData, TargetVisualizerData>()
.Build(ref state);
state.RequireForUpdate<PrefabReference>();
state.RequireForUpdate<EndInitializationEntityCommandBufferSystem.Singleton>();
if (targetPrefab == Entity.Null)
{
var prefabs = SystemAPI.GetSingletonBuffer<PrefabReference>();
foreach (var prefab in prefabs)
{
if (prefab.name != "target") continue;
targetPrefab = prefab.prefab;
break;
}
Debug.Assert(targetPrefab != Entity.Null, "target prefab reference was not found.");
}
[BurstCompile]
public void OnUpdate(ref SystemState state)
{
var ecbs = SystemAPI.GetSingleton<EndInitializationEntityCommandBufferSystem.Singleton>();
}
new InstantiateTargetJob()
{
prefab = targetPrefab,
ecb = ecbs.CreateCommandBuffer(state.WorldUnmanaged)
}.Schedule(_query, state.Dependency).Complete();
}
public partial struct InstantiateTargetJob : IJobEntity
{
[ReadOnly] public Entity prefab;
public EntityCommandBuffer ecb;
public void Execute(Entity entity, ref TargetVisualizerData visualizerData)
{
if (visualizerData.VisualizerEntity != Entity.Null) return;
ecb.SetComponentEnabled<TargetVisualizerData>(entity, false);
visualizerData.VisualizerEntity = ecb.Instantiate(prefab);
}
}
}
[...]
public struct TargetVisualizerData : IComponentData, IEnableableComponent
{
public Entity VisualizerEntity;
}
When i go to set the visualizerData’s entity reference it is set as Invalid Entity, from what i assume, the ecb.Instantiate(prefab) is not being playedback so the reference comes as Invalid Entity during playtime. But i dont see any other way that i can instantiate a prefab and set a reference to it on the same job…

Any idea how i can set a reference to an entity on the same Job, or maybe how to, after set the reference after ECB-Playback.