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

Performance differences of gameObject.Setactive() vs Destroy()

Discussion in 'Scripting' started by Sildaekar, Jan 8, 2014.

  1. Sildaekar

    Sildaekar

    Joined:
    Jul 8, 2013
    Posts:
    95
    I was just curious if anyone has bench-marked the performance differences of gameObject.SetActive() and Destroy().

    Would it be more efficient to just destroy an item and then instantiate it when needed, or to just set active to false then set it back to true when needed?

    My apologies for the poor description.
     
  2. surreal4224

    surreal4224

    Joined:
    Apr 10, 2011
    Posts:
    51
    I would say this depends. If your project will be continuously destroying and creating objects lots of them in a short amount of time, I would say it would be better to make them inactive temporarirly.

    If the destroying and creating of the same object is less frequent or inconsistant, probably better to actually destroy and instantiate a new object when needed to free up the memory.

    just my 2-cents.
     
    bruh0 likes this.
  3. mweldon

    mweldon

    Joined:
    Apr 19, 2010
    Posts:
    109
    SetActive is almost instantaneous. Instantiation is relatively slow.

    EDIT: What surreal said. It is okay to Create/Destroy things as long as you aren't doing it so often that it slows the game down.
     
    Last edited: Jan 9, 2014
    bruh0 likes this.
  4. Sildaekar

    Sildaekar

    Joined:
    Jul 8, 2013
    Posts:
    95
    Thanks for the responses guys, I was hoping for a more detailed answer but this gives me basically what I was looking for. The reason behind this post was for an inventory system I planned on creating for my own game, I decided to just go with destroying and instantiating the items. Being as it is going to be a single player game and I don't plan on the player picking up and dropping 100s of items at a time it seems as though this would be the best method.
     
  5. angrypenguin

    angrypenguin

    Joined:
    Dec 29, 2011
    Posts:
    15,514
    But if allocation / garbage collection is an issue then any creating/destroying in the long run can be an issue.

    If you do it at appropriate times (playing with stuff in an inventory is a good candidate) it's not an issue, though. You only need to avoid it at times where you need stable performance.
     
    Kiwasi likes this.
  6. Ferb

    Ferb

    Joined:
    Jan 4, 2014
    Posts:
    21
    I find that SetActive(false) can take quite a few milliseconds to complete, depending on how many active gameobjects are in the scene. I'm guessing this is because the game engine needs to check for any references to it in any other active gameobjects (like their joints) and remove them. DestroyImmediate takes the same amount of time, which I'm guessing is why the documentation advises not to use it. Destroy, on the other hand, seems immediate, but I've only checked how much time the function call takes, not how long it takes in between frames. But according to the documentation, Destroy is faster than DestroyImmediate, and I do know SetActive(false) is about as slow as DestroyImmediate.
     
    taxvi likes this.
  7. NikFree

    NikFree

    Joined:
    Nov 5, 2017
    Posts:
    1
    If I want to make the enemy disappear when I shot him, should I use Destroy or setactive(false)
     
    UtopianKing likes this.
  8. bruh0

    bruh0

    Joined:
    Sep 3, 2014
    Posts:
    1
    Object.Destroy(obj, time) marks the object for destruction, which will be carried out by Unity magic asynchronously. The time parameter is optional and defines how long the object should wait before being killed off.
    DestroyImmediate freezes execution until it has completed because it is executed this frame. You will rarely if ever need to use this one.

    You have two options:
    • use your own pooling solution (on death -> set inactive, on spawn -> set an old one active),
    • instantiate and destroy normally.
    It depends on your game, but I would stick to the latter. Worry about optmization once it becomes a problem, it is easy to later implement your own pooled instantiate method if needed.
     
    wlwl2 likes this.