GameDev : Destroy()!
Before advance, we have to clean a little bit the code and the Hierarchy.
This is really important, have hierarchy and project clean makes our project more easy to read and if we need something, we find it without lost time.
First, I need to clean up my little mistake.
When I explained how to use [SerializeField], I wrote some variables, I make them private and with that attribute we can see them in the inspector.
This is good, BUT the concept is this: WE ENCAPSULATE attributes and variables within our CLASS, so NOTHING can touch them from the outside.
IF we need it, let’s make it public.
The problem is this: I made some variables visible in the inspector, and this is WRONG.
WHY?
Well. We look again at Player script
Every of this attributes is visible in the inspector.
Is it right?
The answer is no, and here’s why: does it make sense to have visible or laser speeds?
YES, because we drag and drop the reference of laser (prefab) in the inspector, and the if we want change speed we can make it simply from here, we not have to open code every time.
But, does it make sense to have visible the rest of variables?
NO, because we set the bound with precise position, and we won’t change that position, so we make that variables PRIVATE.
Make this simple change
To make things clean, we have [SerializeField] at top, one empty line, then private variables.
Everything readable, everything clean. Good!
Another little problem is this :
Every time we create a laser projectile with Space Bar, it becoming visible in the hierarchy. You imagine if we have a game with many many projectile?
We can’t have every of this in the hierarchy, so, we have to Destroy them!
How can do that?
The game logic is : if laser go out of the screen, destroy it!
So, we need the position out of screen right?
Press Play, shoot, and press Pause
After pause, in the SCENE take the laser and we drag it at the top out of the screen
Now we know that at 7.5f we can Destroy it.
Pseudo Code agin , and type
This is what we want: to fire and when the laser reaches 7.f on Y, it destroys itself.
Firts part of code is simple, we need the If statement
Now, to Destroy it, again look at documentation.
Of course we may not know that the Destroy () function exists, so the first search is on google
If we search for “Unity how to destroy object”
If we look in more deatil, to destroy object we have a method called Destroy()
and can simply pass the object, and a float to setting a TIMER.
For example here, if we write
Destroy (_laser, 3f), means we fire the laser which will destroy itself after 3 seconds!
In our case it is simpler, because we do not need the timer, only that the laser reaches that specific position
Really easy to do, inside IF we type
I explain what happen here :
inside the Destroy method we pass the word gameObject, which refers to the gameObject with THIS SCRIPT ATTACHED.
We can also use the keyword “this” before gameObject
If we Play the game
Every time Laser go out of screen, destroy itself!!
Last thing : to take everything clear, create a variable for bound.
And beacuse for now we don’t use it, delete Start method.
Now we know how Destroy the gameObjects in Unity!
See you in the next articles!