Tip of the day: Destroying parent GameObjects

Keep your Hierarchy tidy!

Instantiating GameObjects to let the player have fun is something essential in every game. However, always keep the things, the player will never see in mind as well. A tidy development area is also a thing you should keep in mind.

Let’s take a look at the current situation at first:

As you can see, there are still leftover GameObjects which are caused by the triple shot laser.

To take care of the leftover junk, we just need to add two lines of code in the Laser.cs script.

We need to check, if the GameObject (here the triple shot laser prefab) has a parent GameObject. If this is the case, we need to simply destroy it. This can be done with the following code:

if(transform.parent != null)
{
Destroy(transform.parent.gameObject);
}

The code inside the Movement() method will look like this now:

If we run the game now and shoot some more triple lasers, we see that the parent GameObjects will get destroyed! More tidied up workspace, yay!

The PowerUp parent GameObjects get destroyed now!

That’s it for todays post. Thank you for reading my posts and showing interest :)

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Timo Schmid

Timo Schmid

46 Followers

The mission? Becoming a game developer! RPG is the dream! Writing down my journey here for me and for everyone interested. Thanks for showing interest :)