Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question DestroyEntity() not destroying children and other weird behaviour

Discussion in 'Entity Component System' started by calabi, Nov 13, 2022.

  1. calabi

    calabi

    Joined:
    Oct 29, 2009
    Posts:
    232
    Shelfobjects2.png
    So I have this above object I'm trying to destroy it has two child objects that get converted along with it. However when I destroy the above object it does not destroy the children which I'm kind of confused with. Also when destroying the main parent object the child objects position gets reset to zero which is another kind of confusing aspect.

    I'm also instantiating a replacement where the children don't get put in the same place as the parent which is another confusing aspect and no matter what I do I cannot get the children to be in the same place as the parent.

    I'm just wondering if anyone can shed some light on this aspect.
     
  2. DreamingImLatios

    DreamingImLatios

    Joined:
    Jun 3, 2017
    Posts:
    3,983
    Does the object have a LinkedEntityGroup? Unlike GameObjects, the transform hierarchy and what gets instantiated and destroyed are two separate concepts in Entities.
     
  3. scottjdaley

    scottjdaley

    Joined:
    Aug 1, 2013
    Posts:
    152
    The only way children get automatically deleted when destroying the parent is if the parent has a LinkedEntityGroup buffer on it. This is added by default to baked prefabs, but not game objects in a subscene. You can get the behavior you want for subscene game objects by adding a "Linked Entity Group Authoring" Monobehaviour to your parent game object.

    When an entity's parent is destroyed, the entity's local position becomes its world position. This means it will teleport if the parent was not at the origin. It sounds like you want to just delete the child, but if you want to delete only the parent and avoid the children teleporting, you have to set the child's transform components such that it matches its world transform at the same time that you unparent it (or delete the parent).

    Here is some code to set the translation, rotation, and scale to avoid teleporting (assuming uniform scale and that you are using transform system v1):

    Code (CSharp):
    1. UniformScaleTransform ltwTransform = UniformScaleTransform.FromMatrix(ltw.Value);
    2. translation.Value = ltwTransform.Position;
    3. rotation.Value = ltwTransform.Rotation;
    4. scale.Value = ltwTransform.Scale;
    After instantiating the replacement, are you setting the position (Translation) of the newly instantiated replacement? You should only have to do so on the parent of the replacement.

    Note: I'm assuming you are using the transform system v1. This is true if you are on 0.51 or earlier or 1.0 and using netcode or physics. If none of those things are true, you are on v2. This means that components like Translation, Rotation, and Scale don't exist. Instead you have a LocalToWorldTransform which changes how you do things.
     
  4. calabi

    calabi

    Joined:
    Oct 29, 2009
    Posts:
    232
    It doesn't so I guess I'm going to have to add it manually and then add the children to it manually that's going to be kind of annoying because in one case I have over 100 children to add.

    Ok that makes sense for destroying the entities a child is converted directly into world position it just means I have to make sure I destroy the children I guess. Its kind of crazy though in all my time using DOTS I haven't encountered these problems before.

    But still the issue with instantiating and its children not moving with it persists, could it also be because the entity does not have a linkedentitygroup. Is there not a way to automatically add that component.

    Thanks both for help.
     
  5. DreamingImLatios

    DreamingImLatios

    Joined:
    Jun 3, 2017
    Posts:
    3,983
    There's a LinkedEntityGroupAuthoring to ensure it gets added to scene entities. Otherwise, it is only automatically added to prefabs.
     
    calabi likes this.
  6. calabi

    calabi

    Joined:
    Oct 29, 2009
    Posts:
    232
    I think I understand the problem with instantiating the child objects, they have a physicsbody and it says they will be unparented during the conversion process. So then that leaves me kind of confused I want them to immediately have velocity when instantiated, its approx 200 objects, is the only way to iterate through them all and position them individually before they go flying off, is that even possible.
     
  7. scottjdaley

    scottjdaley

    Joined:
    Aug 1, 2013
    Posts:
    152
    Parenting and physics don't play nicely with each other and they make it tricky to do certain things at authoring time.

    If I understand correctly, you have a prefab with a bunch of child objects. At runtime, you want to place the entire prefab and all children in a specific position at once and then have each child object go flying off with a velocity. And the problem is that the parent-child relationship is lost in baking because you have physics bodies on the children. Is that right?

    I don't know if there is a way to disable the physics system unparenting during baking like that. If you want to work around this, you could add some components in authoring that restore the parent child relationship on your baked prefabs at runtime right when the game starts. Then when you instantiated the prefab, everything will be parented like you want. But, its important that you unparent the children (and update their position, and rotation so as it not teleport) before they go flying off for physics to behave correctly.
     
    calabi likes this.
  8. calabi

    calabi

    Joined:
    Oct 29, 2009
    Posts:
    232
    Interesting that is probably the best more efficient way, but I did just try the instantiate and set the positions of 200 physics objects and it seems to work ok so maybe I'll just leave it like this for now.