We Destroy() everything.... should we?

yet another video where if we want it out of the hierarchy, we destroy it…

I thought destroy causes frame rate issues later because of garbage collection (which I don’t understand, I just learned it was bad), and to instead use pooling or SetActive false to GO that you no longer want in the scene… that it is better in the long run… is this not correct anymore with the newest versions of Unity? if it’s still best practices to do this instead of Destroy(), why on earth aren’t we covering alternatives to Destroy()? A project this big with a lot going on in a scene, won’t Destroy() be an issue? On desktop? on console? on mobile?

@Rick_Davidson
@sampattuzzi

Destroy() can be an issue, but so can having too many unused GameObjects in your scene.

An actual better solution when you’re dealing with things like Projectiles is to create an object pool, a variant of the Factory model. Rather than instantiating an object from thin air with Instantiate(prefab), you have a factory that provides arrows. The factory will maintain a list of used objects. When the game needs an arrow, it asks the factory. If there are no used arrows, it Instantiates one and returns it, if there are used arrows, it resets everything on the arrow and returns it. Instead of destroying the arrow, you return it to the factory, which deactivates it and stores it in the list of used objects, waiting to be refurbished.

2 Likes

so pooling is indeed better… why all the Destroy() in this course?

Pooling is not something we’re teaching in the RPG course. It’s a more advanced topic that distracts from the core of the course.

2 Likes

Exactly. It takes a little work to set up and distracts. But yes, it’s the better option if you intend to spawn lots of projectiles or you see through testing that this is slowing down your game.

3 Likes

My two cents:

Unity, since 2019 is using another system for garbage collection which makes everything smoother, so you don’t really have to worry unless you are spawning way too much things. Here’s a little explanation on how the system works and what’s different, it also explain what a Garbage Collection is and how it works.

And here’s an amazing tutorial that teaches how Object Pooling works and how to code it step by step from the Unity Team, it’s a little long but I like it way better than those tutorials that last less than half this tutorial but assume way too much from the student.

An easy way to test this out is by going back to one of the Unity courses projects: Glitch Garden or Laser Defender from the Unity 2D course, for the 3D course Argon Assault or Realm Rush work fine.

4 Likes

awesome, I’ll watch these… usually I use Core Game Kit for pooling, but knowing how to code it myself may be helpful, and I’ll definitely learn from that new garbage collection vid, thank you!

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms