Search Unity

  1. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  2. Improved Prefab workflow (includes Nested Prefabs!), 2D isometric Tilemap and more! Get the 2018.3 Beta now.
    Dismiss Notice
  3. Want more efficiency in your development work? Sign up to receive weekly tech and creative know-how from Unity experts.
    Dismiss Notice
  4. Participate with students all over the world and build projects to teach people. Join now!
    Dismiss Notice
  5. Build games and experiences that can load instantly and without install. Explore the Project Tiny Preview today!
    Dismiss Notice
  6. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice
  7. Want to see the most recent patch releases? Take a peek at the patch release page.
    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:
    785
    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:
    655
    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. }
     
    HeyZoos and B0dg3 like this.
  5. Bhakti_GL1

    Bhakti_GL1

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