Script Communication in Unity using GetComponent!

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.

--

--

--

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

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

How to Write Recursion in 3 Steps

Pre-Commit Hooks DevOps Engineer Should Know To Control Kubernetes

📢Medium guide

From Rails to Clojure, then to Java, then back to Rails

MongoError: E11000 duplicate key error collection ??

Data Ecosystem and Software As A Commodity

Assistant & … Pizza 🍕 — Overview — Part 1

Ranges in Ruby

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Michel Besnard

Michel Besnard

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

More from Medium

Initializing A Git Repository in Unity

Creating Elevator in Unity — Part 3: Moving Elevator.

2D Mobile: Setting Up The Player

Virtual Cameras w/ Cinemachine in Unity 2020 — Part 4