Project 1: Space Shooter
How to access another script’s methods in Unity
By introducing health and damage to the Player object

Thus far my player has been immortal, but today I will remedy that by giving some health to my player. This is done by simply adding a new private integer type variable called _lives to my player and setting it to some value, three lives seems quite common so three it is.
The method of dealing damage is as simple as the amount of lives is. All I have to do is to make a public method type void called Damage and reduce the _lives by one every time the method is called, and an if-statement to check if the _lives has reached zero so that the Player object can be destroyed.
Remember to type the keyword public before the type and name of the method, because by default all the methods inside a script are private and cannot thus be accessed from other scripts.

So how to get my Enemy object’s script to access this Damage method? Since the argument of the OnTriggerEnter gives access to the colliding object, named other by default, I can access the Damage method inside the Player script by finding the Transform component and using a method called GetComponent to find the Player script and then access the Damage method.

Now, if for some reason the Player object does not have a script called Player, the GetComponent method will return null as its value. Unless it is checked if the GetComponent returned what I wanted, will Unity produce an error and in the worst case cause the game to crash. By doing these checks the game, or a program in general, keeps on running, it just misses some features.
