Search Unity

  1. Unity 2019.1 beta is now available.
    Dismiss Notice
  2. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  3. We're looking for insight from anyone who has experience with game testing to help us better Unity. Take our survey here. If chosen to participate you'll be entered into a sweepstake to win an Amazon gift card.
    Dismiss Notice
  4. Want to provide direct feedback to the Unity team? Join the Unity Advisory Panel.
    Dismiss Notice
  5. Unity 2018.3 is now released.
    Dismiss Notice
  6. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice

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

Discussion in 'Entity Component System and C# Job system' started by Ryetoast, Oct 14, 2018.

  1. Ryetoast

    Ryetoast

    Joined:
    Mar 8, 2015
    Posts:
    18
    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:
    937
    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:
    863
    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:
    18
    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. }
     
    f1r4tc, 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.