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.