Scripts talking to other scripts
There will be instances when you have to reference methods from one script to another script. For example, having the enemy script communicate with the player script to check when a player loses a life upon collision.
Getting Components
By default, the only component you have direct access to is the “transform” component. To grant access to other components, you would need the script “GetComponent.”
Once you have access to the desired component, you can retrieve any component within the object, in this case we wanted the damage method.
Null Checking
Null checking is a crucial method of double checking for mistakes. Especially when trying to grant access to components that might not exist or maybe its a bad link. In worst cases scenario, having a non existent component might lead to errors or crashing of the application.
To prevent that from happening, we can create a variable that contains the component, then have an if statement to null check if component is indeed active.
Now when the script check the component and it doesn’t equal to null, it will execute the player damage method.
TLDr: Null checking is important and every developer should get into a proper habit in doing it, because it prevents fatal errors.