In the last article we created a Triple Shot Powerup, and our Prefab consisted of 3 Lasers inside of an Empty Game Object (the parent).
In our Laser code, when they go off-screen they get destroyed. However, this still leaves our Parent hanging around in our Scene Hierarchy:
We could create a separate script for the Parent Triple Shot, but there is a much better and cleaner way to get rid of these Parent objects.
Destroy Parent Object code
Inside our Laser script, all we have to do is a simple null check to see if a Laser is attached to a Parent Object, and if so, destroy the Parent, which in turn will destroy the Children as well (our 3 lasers).
Here is the code:
So, if transform.parent DOES NOT equal null, meaning if there is a parent to the Laser, then Destroy(transform.parent.gameObject).
Now our Hierarchy is cleaned up and looking nice again! No more jumble!
Next we’ll create a Speed Boost Powerup! Gotta’ go fast!!