Day 12: Unity Script Communication using GetComponent

Blake Zoeckler
3 min readMay 1, 2021

Often we require our scripts to be able to communicate with each other to tell one another to update when certain events happen. One script may detect that event, and then we may want to have that script tell other scripts what they should do in that event.

For example, in the space shooting game we’ve been working on, the player should take damage when an enemy collides with the player.

Here we introduce a new variable in the Player script called _lives. This keeps track of how many more times the player can take damage before being destroyed.

The “public” keyword is very important here!

Damage is a new method in the Player script. When the function is run, it will remove one life from the player, and then if the number of lives is 0 or less, the player will be destroyed.

However, the code to detect a collision is only in the Enemy script. How can the player script know when it should be damaged?

To solve this, we ensure that this new method is public. This means that any other script that is able to access this Player script will be able to use the Damage method to incur damage on the player.

Add the two highlighted lines in the Enemy script.

Now we go to the Enemy script, and modify the OnTriggerEnter2D method. The first line uses the very useful “GetComponent” method, which can get any type of component from a given object. In this case, we use it to get the attached Player script from the “other” object that the enemy collided with. Then, we can call our new Damage method on the Player script. In this way, one script is able to access and affect another one! We can see the final results here:

We can see the player’s lives decrease as it collides with enemy squares. Once it hits zero, the player is abruptly destroyed, leaving us with nothing to do except reset and try again.

However, there’s actually still a big issue here. The script that is requested by GetComponent might not actually exist. If that happens, trying to call the Damage method on a nonexistent script will cause an error like this:

As we get more and more scripts, things like this might potentially happen, so it’s important that we handle this case properly.

Here we can fix it by including an if-statement. “If (playerScript)” checks to see if the player script actually exists. If it does, then we can call the Damage method on that script. If it doesn’t (which can happen if the other collided object doesn’t have a player script) then instead we just skip that damage step, and only destroy the enemy.

Using GetComponent is a very powerful tool, but we have to use it responsibly. If there’s any properties that only a certain class should use then it’s important to ensure we keep define those variables or methods as private. Otherwise, any script we write could potentially access those things and mess with them, which just adds a lot of confusion for no reason.

--

--

Blake Zoeckler

I’m a passionate and talented software engineer seeking an opportunity in game development.