Script Communication Using ‘GetComponent’
When a trigger collision occurs with the Enemy object, the information about the colliding object is stored in the ‘other’ variable of the OnTriggerEnter() method. we create the ‘other’ variable, it can be named as we see fit.
if (Other.tag == “Player”) is checking to see if the tag on the ‘other’ variable is logically equivalent to the string ‘Player’. the “==” is a Boolean operator an will return true or false.
Below we see the Tag for the Player object is set to “Player”
All of this code is in the Enemy script. However, the lives for the player are in the Player script, nested in the Damage() method. for us to communicate with the Player script, we need to open a pathway between the two components (remember the script is a component of the gameObject).
We have direct access to the transform component. we can simply type other.transform and access the Player gameObject transform component.
However, if we want to access a different component. we have to use the .GetComponent function.
We can see from the inspector that the script component for the Player gameObject is labeled “Player”. We know that the gameObject information is stored in our variable ‘other’. To use the .GetComponent function we simply need to identify the component we want.
Looking at the code: Other[the object we want].transform[the only component we have access to].GetComponent[retrieves the component] <Player> [component retrieved]
Now that we have the pathway to the Player component open we can call the methods in the Player Script using our player object. player.Damage() calls the damage method inside the Player script.