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

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.

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.

1 Like

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.

1 Like

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.

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.

1 Like

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.

1 Like

If I want to make the enemy disappear when I shot him, should I use Destroy or setactive(false)

1 Like

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.

1 Like