Script Communication in Unity using GetComponent!

Michel Besnard
3 min readMay 28, 2021

Objective: Learn how to communicate between scripts and access their methods.

https://images.app.goo.gl/yFxFFTGwkUrr7VXn9

It took me a while to understand the concept of script communication in Unity.

The “Transform” component of an object is the only component you can directly access from any script. If you want to access another component (Mesh Renderer, Collider, Script,…) you need to use the GetComponent method to access the GameObject and tell Unity which component you are looking for.

The object with which we collided was stored in the variable “other”, this was done by calling the function OnTriggerEnter(Collider other). “other” stores the name of the object with which we collided. We then look at the tag of the GameObject stored in “other” and execute an IF statement to verify that “other” is equal to “Player”. If that tag is equal to “Player”, then you run the code within the IF statement.

other: variable which stores the name of the object we were looking for via their tag.

transform: the root of the “other” object.

GetComponent<PlayerScript>: looks for and gets the component that is inside the T-brackets (<nameOfComponent>) called “PlayerScript” which happens to be the public class (script) component attached to the object, and initializes it with ().

Damage(): this is the method we are looking for within the “PlayerScript” component.

It’s all done in a very structured hierarchy. You’re telling Unity to look at the “Player” GameObject (“other”), look then at its Transform, then get the component called PlayerScript (the script), and then within that script, run the Damage() method.

Error handling with null check

What if you are trying to access a component that doesn’t exist? In the example below, the red cylinders (think enemy here) are colliding with our white cube (player). OnTriggerEnter() is called from the Enemy class, determines the collision occurred with a GameObject tagged as “Player”, then tries to access the PlayerScript class attached to the white cube to call the player.Damage() method. Unfortunately, the PlayerScript component is missing from the “Player” GameObject. Unity can’t find it!

Best practice requires that you do a null check. This error handling will prevent crashes from occurring when you are trying to communicate with a missing component.

In this example, let’s store a reference to the desired component, which is our public class (script) called “PlayerScript”, into a variable called “player”, and attempt to run the Damage() method without doing a null check. It’s all fine and dandy as long as the PlayerScript class exits.

Calling a method without performing a null check.

In order to confirm the desired component is present, you use an IF statement to verify that the “player” variable is not empty. You do this with a null check as depicted below. Only then should you be calling the desired method.

If “player” is not empty (not equal to null), then run the Damage() method.
Null check implemented.

In summary, we’ve looked how to setup communication between scripts using the GetComponent<T>() function. Keep in mind that GetComponent is not the most efficient method of communicating between scripts. Best practice, I believe, calls to cache a reference at initialization in the Start() function because calling GetComponent can become fairly expensive in memory and performance.

--

--

Michel Besnard

Military member with 35+ years of service, undertaking an apprenticeship with GameDevHQ with the objective of developing solid software engineering skills.