Today we are going to learn how to communicate between scripts in Unity using the GetComponent function.

Unity's API description of GetComponet specified that it Returns the component of Type if the game object has one attached, null if it doesn't.

On our space shooter game, we will need to be able to communicate between scripts to damage and destroy the player.

On the Player script, we need to create a function for the players lives.

We also need to create a public class to handle the damage of the player.

--

--

Today we are going to create our cool down system, so the player cannot fire the lasers as fast as he can press the space key. First of all, on the Player script we are going to create two private bools, and we are going to name it “_fireRate” and “_canFire”.

At this point, we are going to create an “IF” statement to run our logic. This will allow the player to fire the lasers with a fire rate of 0.5 seconds.

--

--

Today we will learn how to instantiate game objects and also destroy them. To begin, we are going to create a game object and name it Laser. Then, we need to convert it into a prefab.

Now, that we create our laser, we need to jump back into our player script. We need to create a public GameObject and name it _laserPrefab. In the void update method, we are going to create an “IF” statement to get access to the input manager and inside the curly brackets we are going to instantiate the _laserPrefab.

--

--

It is always a very important practice to write pseudo code especially if you are a person like me that just started to learn how to write code.

This practice will help us, especially because it will show us in plain English what it is that we are trying to achieve with our code. For example, if we are writing a script of an enemy in our game and we want this enemy to move with the speed of 4-meters per second, we will write these instructions starting with a doble forward slash like this//

Like I mentioned at the beginning of this article, pseudo code is very important, especially for beginners, at coding and programing.

--

--