Script Communication in Unity using GetComponent

Unity is a system that consists of ‘Components’ that define the properties and behaviors of every GameObject. We can access and work with our components' values in our scripts.

We will be doing this via the ‘GetComponent()’ method. This method allows our scripts to communicate with each other, and that is what we will be talking about today.

How can scripts communicate with each other?

You can only communicate with another script when you have access to that script. If your scripts are on the same GameObject, then the access is pretty straightforward.

If they are on different GameObjects, you will have to create a reference to the other GameObject.

There are a few different ways how you can get access to another script.

One GameObject, Multiple Scripts - In this case, you can cache the reference to the other script.

Two GameObjects, Two Scripts - In this case, you will have to find a reference to the other GameObject.

  • Create a direct reference to the other GameObject.
  • Use the Find() method to find a GameObject via its name.
  • Use the FindGameObjectWithTag(s)() method to find GameObjects via their tags

Once you reference the other GameObject’s script, you can then call or adjust any public methods/variables.

But that is it for now, thank you for reading and feel free to follow me for more articles — and as always, good luck and see you next time!

Aspiring and eager beginner Unity developer

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