Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Performance Deleting Objects

Discussion in 'World Building' started by lennartkueppers, Dec 27, 2018.

  1. lennartkueppers

    lennartkueppers

    Joined:
    May 23, 2018
    Posts:
    20
    Hey,
    I have a very simple question. I have a few room prefabs, all with different children like for example lights, raycasts, colliders and so on...Now, In my case These rooms are procedurally connected to each other and you can increase the amount of rooms. During the Placement of the rooms I am using some of these children and sometimes disable them when they are not used anymore...
    Now my question...Is it better to disable these specific children, or when they are invisible just Keep them, or delete them? Deleting/Disabling them of Course costs some more time but at the end there will be less objects...Can you tell me what is the best choice (regard to Performance, structure and logic)?

    Maybe a General answer would be the best^^
    Thanks
    Lennart
     
  2. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    Disabled objects only have a reference and memory cost, so these will be higher performance than destroying them.
    Some comparisons:

    • destroy - costs performance, usually on main thread
    • disable - can incur allocations when enabled again for some things and not others (like colliders), generally a preferred choice
    • UnloadAsync methods - for streaming a scene in and out, it's possible to "destroy" in a friendly manner
     
    Ryiah, lennartkueppers and Antypodish like this.
  3. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,753
    I would like to add, if memory is not an issue, and you expect to reuse an objects, is worth to keep them, by only disabling, as hippo indicated. Then you just enabling it, and you can reuse it. For example bullets. Or debris.
     
    Ryiah and lennartkueppers like this.
  4. lennartkueppers

    lennartkueppers

    Joined:
    May 23, 2018
    Posts:
    20