Search Unity

GameObject with GameObjectEntity isn't cleaned up when entity is destroyed

Discussion in 'Entity Component System' started by Ryetoast, Oct 14, 2018.

  1. Ryetoast

    Ryetoast

    Joined:
    Mar 8, 2015
    Posts:
    48
    I realize this won't really be a problem once things are all pure ECS, but right now it seems strange that this is true.

    Has anyone else dealt with this issue in a good way? I was thinking of just making a system that grabs all GameObjectEntity components and checks if the entity exists, and then deleting the game object if not.
     
  2. 5argon

    5argon

    Joined:
    Jun 10, 2013
    Posts:
    1,555
    Managed <-> ECS is not really symmetrical at the moment. On enabling/disabling game object and it creates and destroy the corresponding Entity : this is possible because the game object stored the Entity and it works on the current World.Active. It works because there is a event receiver method on enable/disable.

    You can use ISystemStateComponentData to detect Entity's disappearance and destroy the corresponding game object by checking for the hybrid GameObject component. But there would be a bit of moment that the Entity does not exist but game object still exist before this clean up system can run (a sync point). I think it is because of this not so seamless design they decided not to implement it, unlike the other way around where we can have/lost Entity immediately.
     
  3. eizenhorn

    eizenhorn

    Joined:
    Oct 17, 2016
    Posts:
    2,684
    And this obviousley, entities is ientities, game object is game object, they live in different “universes”, even if they can call each other :)
     
  4. Ryetoast

    Ryetoast

    Joined:
    Mar 8, 2015
    Posts:
    48
    Here's what I ended up with in case someone wants a simple solution. It's not particularly efficient, but since it is ideally temporary I'm not too worried about coming up with something better.

    Code (CSharp):
    1. class GoCleanup : ComponentSystem {
    2.     protected override void OnUpdate () {
    3.         var goes = Object.FindObjectsOfType<GameObjectEntity>();
    4.  
    5.         foreach (var goe in goes) {
    6.             if (!EntityManager.Exists(goe.Entity))
    7.                 Object.Destroy(goe.gameObject);
    8.         }
    9.     }
    10. }
     
    firatcetiner, HeyZoos and B0dg3 like this.
  5. Bhakti_GL1

    Bhakti_GL1

    Joined:
    Jul 4, 2018
    Posts:
    17
    I fix that "issue" by destroying entity gameobject when its entity is removed.