How to destroy an entity without getting a error

Whenever I try to destroy an entity I get en error because the entity that was destroyed was being referenced by something (prob. a ComponentSystem doing a forEach loop on it). seconds after that Unity crashes.

how can I deal with this? Do I unreferenced the entity? if so, how do I do that?

If you can make Unity crash just by deleting an entity, you should file a bug report, crashing the editor is a big deal.
If you want to delete an entity frfom inside a job or Entites.ForEach() lambda I think you’re supposed to use an entity command buffer. So you are actually scheduling the deletion for later when the loop is done. Which ECB system to use I guess depends on in which UpdateGroup your System is executing. I think you have to find the command buffer system to use, then request that it create a new buffer, and pass that buffer into your job, something like:

EntityCommandBuffer ecb = World.GetOrCreateSystem<EndSimulationEntityCommandBufferSystem>().CreateCommandBuffer();

Then inside your job

        ecb.DestroyEntity();

Don’t take my word for it, Unity has its Unite Copenhagen video on entity command buffers

1 Like

well I mean, ever since I started using ECS Unity has been crashing left and right. I want to report those bugs but its so hard to point out what the problem(s) is or Are.

Sometimes it crashes immidietly after runtime, sometimes its 5 seconds after runtime, sometime(rarely) it doesnt. Sometime it also crashes after I updated a script and when I’m just clicking a GameObject.

I really dont want to use ECS but my game kinda needs it. I’m not really good at programming since I’m more into the art side. ECS is so hard to learn since the documentation is lackluster at best and people in the forum just assume you already know alot about ECS(even tho I dont) when answering ur questions about specific part of ECS and so alot of the time they dont help.

That Unite video barely gives a tutorial on how to actually implement ECB(not alot of code example on screen). So they’re not very useful if u arent good at programming and u have a specific use case for ECB.

anyway, that was my rant on ECS. It’s been hell trying to get ECS to work on my game and I’m barely half way done