GameDev : clean Hierarchy

We have now to clean our hierarchy, because when we collected Powerup and shot, a tons of game Objects remain in the hierarchy, and this is not good.

Te easiest way we can do this is open the Laser script, and here create a logic where we destroy directly the PARENT of the object

So when we fire the Laser, it self-destructs when it exits the screen.
In Triple Shot, we have a Parent object, and the three Lasers are the Child.
And we need to destroy the parent too!

So here, we need another if, and we check if the object has a parent

And if it has a Parent, destroy it

The logic inside Destroy is to try to get to the gameObject Parent, that’s why we start with transform.parent, to get to the parent object, but we have to DESTROY the GAMEOBJECT ATTACHED TO THAT TRANSFORM, NOT THE TRANSFORM ITSELF.
I say this, because, especially at the beginning, it can happen to think : ok, I reach the parent, now destroy it.
But this gives us an error, because we CANNOT DESTROY THE TRANSFORM but only the GameObject attached to that Transform.

Just for example

And press Play

Unity give us this error
“Can’t destroy Transform component of ‘Triple_Shot_Test(Clone)’. If you want to destroy the game object, please call ‘Destroy’ on the game object instead. Destroying the transform component is not allowed.”

That’s how after transform.parent we need to add gameObject.
To destroy the GameObject parent.

Play it

No error, and hierarchy is now clear.

If you prefer we can use this in front of this line of code.
Intellisense said to us that this code che be simplified

The code can be semplified

If at the end of this line we press Alt+Enter and press Enter when happear mini menu

Can we remove the keyword THIS

We can remove from everywhere if we want.

I think the “this” keyword is mainly used when creating class CONSTRUCTORS, in this case it is not necessary.
But again, if we use it is not WRONG.

Always in love with programming, even if late (I'm already 39 years old) I decided to follow my dream! We will see how far my passion will take me!