Learn to code by making a 2D space shooter game.

Objective: Give the enemy Laser the ability to damage the Player.

The first step lets create a Player variable type called _player inside the laser script.

The second step lets assign the component to the variable inside the start method, we do this by finding the game object then getting the component.

If you don't know to communicate with a script here is an article https://suleimanabdullah.medium.com/learn-to-code-by-making-2-5-d-space-shooter-cb1c4c67213b

The next step is to detect collision between player and EnemyLaser, we do this by adding OnTriggerEnter2D and checking if a tag is a player and if _isenemyLaser equal to true.

In the next step, we want to communicate with the player in order to call TakeDamage from a player script.

But the best practice working with GetComponent is to null check to avoid crushing your application.

Let's see the result.

Plus a bonus add quit option when you are stuck in full-screen mode, add this code to script which won't be destroyed

See you in the next article.



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
Suleiman Abdullah

Self taught Unity Developer, who is passion about making games ,and he loves Math For Game Development