Firing too many lasers. How to destroy objects.

Brian Branch
2 min readApr 1, 2021

Our last article set up our Player to fire lasers, and it looked pretty good. But I want you to have a look at something in the Hierarchy window while we are firing.

Our lasers are just piling up in the hierarchy, which means they are not disappearing. What we need to do is destroy them when they go off-screen. Luckily there is a method for this called Destroy. We can use an “if” statement to check if the laser is off-screen and if so, we can destroy the object.

The code for this would look like this.

Now that we have our code in, let's see how it looks now in the Hierarchy.

That’s much better now. The only problem left is that the lasers are firing too quickly. We can fire as fast as we can hit the space bar. In the next article, we will add a cool-down timer to limit how much we can fire. Until then, good coding, and enjoy your journey.

--

--