I want to add a newly created testEntity to the LinkedEntityGroup of MyCarEntity.
As soon as MyCarEntity gets destroyed, **testEntity “**should” get destroyed too.
This code works, but as soon as I destroy MyCarEntity, testEntity still exists.
If I check my Entity Debugger, testEntity also exists there after destroying MyCarEntity.
And if I go to the last page of LinkedEntityGroup of MyCarEntity (before destroying), the editor window gets somehow broken (in Unity 2021.1.0b6).
Also the index of testEntity is displayed wrong there, its displayed as “-8”? (But actual testEntity has id 231)
Most likely you have assigned entity, just after ECB have instantiated it (still in the same frame).
Meaning entity does not exist yet, but have temp entity index as negative value.
So you assign entity with negative index. Frame later entity is created, but the reference stays invalid.
You should assign entities, after ECB is played back / next frame.
Thanks for the explanation. That makes totally sense, but it also means a lot of extra work or? I thought maybe about an extra system for this, which looks for an extra component on testEntity, which holds the Entity to add the buffer item later on…
That worked perfectly! Thanks for this suggestion Nice to know that there are already some build-in solutions for such cases ^^
In such cases, sometimes I have component on new entity, called AddToParentComponent, with parent entity as value. This component is added by ecb, or is part of instantiated prefab via ecb. Just need set parent entity to that component, which is assumed to exist.
Then there is separate job, which executes only for entities with that component.
It handles parent addition and maybe some relevant tasks, if required.
At the same job, I remove that component, so job is not called anymore for that entity.
The down side is, this may create frame to two frames delay, depending on the design.