Tip of the Day: Communication 101 in Unity

Mohamed Hijazi
2 min readMar 21, 2021

The building blocks of the Unity Engines are the scripts, and these scripts mostly should be attached to a gameObject in order for this script to function.

What if you want two scripts on the same gameObject to talk with each other? Or two scripts on different gameObejcts?
There are many different ways to allow the scripts to communicate with each other, allow me to mention a few.

If two scripts are attached to the same gameObject (then they are basically two different components on that gameObject), so from the first script, you cache the second script and then you get the component of this second script in the Awake or Start methods.

If two scripts are on two different gameObjects, then from the first script you cache the second script, “Find” the other gameObject and then get the component script from it. Let’s mention two ways to do so.

Find the other gameObject by its name

You need to be careful, the string is case sensitive.

Find the other gameObject by its tag

Finding with tag is much more efficient

If two objects are colliding, then the object collided with is cached in the collision variable and can be used to communicate with the other script.

Without getting into too much details now, there is another way to communicate with scripts. If you know there is only going to be one instance of an object (UI for example), then you can create a singularity. Let’s leave this for another article.

--

--

Mohamed Hijazi

A knowledge seeking biologist who is following his passion into a full time career in Unity / Game Development. https://www.linkedin.com/in/mohamed-hijazi/