Dev Day 23: Script Communication in Unity3D using GetComponent
In the previous article (link is at the end of this article), we destroyed our Enemy GameObject after it collided with the Player GameObject by writing an If statement using Destroy(this.gameObject) in the Enemy script.
However, nothing happened to the Player GameObject. What if we wanted to damage the Player GameObject three times before destroying it when it hits the Enemy GameObject? We could create a variable for the Player’s lives, define the variable, subtract a life every time the two GameObjects collide, and then use an If statement in the Player script using Destroy(this.gameObject). But how would the Player script know when the two GameObjects collided to begin subtracting lives? This is where we would use Script Communication.
Script communication in Unity3D is a useful approach to creating game logic through the use of the GetComponent function, which allows one script the ability to access different functions from within another script.
Let’s set this up.
In our Player script, we will create a private int variable called _lives and have it equal to 3. We can also use [SerializeField] if we want to change this in Unity.
Next, we will create a new method called Damage(), create a statement to subtract a value of 1 from the _lives variable, and create an If statement that states if the current _lives variable becomes less than 1, we will destroy this Player GameObject.
Now, we need to call this Damage() method from the OnTriggerEnter method in the Enemy script by accessing the Player script component within the Player GameObject’s Transform. We have already defined the other.tag as the Player GameObject within the OnTriggerEnter method, so we can use the other variable to gain access to the Player GameObject’s Transform and use GetComponent to access the Player (Script) component. This is what that line of code would look like:
But before we access the Damage() method in the Player script, we will want to verify that the Player (Script) component actually exists using a Null Check. A Null check is a way that we can avoid errors in our build by first verifying that the assets we are communicating with are valid. To set this up, we will first create a variable of type Player and name it player, which will be equal to our GetComponent line of code, which looks like this:
Then, to Null Check, we will create an If statement using the new player variable we just created, saying: if the Player (Script) component is not equal to (written as !=) null, meaning the Player (Script) component exists, then run the Damage() method from the Player script.
Here is what the whole OnTriggerEnter method looks like now:
Every time the Player GameObject and the Enemy GameObject collide, the Enemy GameObject will be destroyed, and the Player GameObject will lose one life until its life count is less than one, which at that point, the Player GameObject will be destroyed.
If we save our code, go back to Unity, and play the scene, we will see just that.
Dev Day 22: Creating An OnTriggerEnter Event In Unity3D https://petethomasberger.medium.com/dev-day-22-creating-an-ontriggerenter-event-in-unity3d-7ea5f1a296fa