I am new here and I have already spent some time looking a solution in this forum and examples, but I might have missed it.
I destroy entities when their age reach some threshold (this is temporary code to test things)
This code was extracted from the ECS examples, and it works. Entities are destroyed and disappear from the scene exactly as expected.
public partial class LifeTimeSystem : SystemBase
{
EntityCommandBufferSystem ecbs;
protected override void OnCreate()
{
ecbs = World.GetOrCreateSystem<EndSimulationEntityCommandBufferSystem>();
//ecbs = World.GetOrCreateSystem<EndFixedStepSimulationEntityCommandBufferSystem>();
}
// OnUpdate runs on the main thread.
protected override void OnUpdate()
{
var commandBuffer = ecbs.CreateCommandBuffer().AsParallelWriter();
var deltaTime = Time.DeltaTime;
Dependency = Entities.ForEach((Entity entity, int nativeThreadIndex, ref CropState cropState) =>
{
cropState.age += deltaTime;
if(cropState.age > 10.0f)
{
commandBuffer.DestroyEntity(nativeThreadIndex, entity);
}
}).ScheduleParallel(Dependency);
ecbs.AddJobHandleForProducer(Dependency);
}
}
Howerer, once any entity has been destroyed, instancing a new code triggers a crash with this message :
ArgumentException: All entities passed to EntityManager must exist. One of the entities has already been destroyed or was never created. EntitiesJournaling may be able to help determine more information. Please enable EntitiesJournaling for a more helpful error message.
New entities are instancied from monobehavior, one at a time, by the EntityManager.Instantiate method.
Where is my error?