1
\$\begingroup\$

I read about both methods in Unity and wanted to ask about which was the best in efficiency : destroying the object or deactivating it. What is the performance difference?

\$\endgroup\$

2 Answers 2

7
\$\begingroup\$

Destroying the object gets rid of it completely. You cannot get it back. It is gone.

Deactivating it just disables it; everything is still there, it just does nothing.

Therefore, if you want to reuse the object, you can deactivate it, but if you will never use it again, you should destroy it (and get back its memory).

\$\endgroup\$
1
\$\begingroup\$

You should also mention that creating objects isn't cheap performance-wise. So you should be reusing objects that you plan to have multiple of. In general any game object that is being created and destroyed over the course of the whole game should be reused and not destroyed.

You can google object pooling for more information.

https://unity3d.com/learn/tutorials/modules/beginner/live-training-archive/object-pooling http://unitypatterns.com/resource/objectpool/

\$\endgroup\$
1
  • 6
    \$\begingroup\$ While this is good advice, I'd like to offer a word of caution. I see a lot of newcomers to Unity slavishly (re)implementing engine systems in what they've heard is "the right way" - object pools, dependency graphs, etc. - spending weeks on best-practice fixes for performance issues their game doesn't have, because they haven't made their game yet! So if a new user is reading this, I'd recommend diving in & building your gameplay first, find the fun, and then profile to see if object creation/destruction overhead is a noticeable bottleneck on your target platforms. If so, you can fix it then. \$\endgroup\$
    – DMGregory
    Commented Jun 17, 2015 at 13:43

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .