Script Communication in Unity using GetComponent

Gert Coppens
Nerd For Tech
Published in
3 min readMay 22, 2021

--

A Component is the Base Class for everything attached to GameObjects.

It defines every aspect of a GameObject and its behaviour. Basically any script attached to the GameObject compiles as a Type of Component.

However, if we only wish to store data we can create an accessible class called ScriptableObject that is not directly attached to the GameObject. We will definitely look into this later but click here if you wish to learn more about it already.

If you want to know more about using a particular Component you can always access the reference page for a Component by clicking the small in ? on the Component’s header in the inspector.

Tip: The Type and current property settings of a component are stored when you use the Copy Component command. These can be pasted into another component of the same type with Paste Component Values or can also be created as a new component with its respective values and settings by Paste Component As New. Since recently you can also copy paste the Position, Rotation and Scale separately.

Script Communication

GetComponent()

Unity provides us with a method that returns the Component of a GameObject that we want to get in order to access its properties or behaviour.

Returns Component of Type ‘type’ if GameObject has one attached and “null” if it doesn’t, disabled GameObjects included.

Declaration & Generic Declaration
Use example

GameObject.Find()

In order to get a component on a different GameObject, we can use GameObject.Find to get a reference to the other GameObject and then use GameObject.GetComponent.

Finds a GameObject by name and returns it

This is useful for automatically connecting references to other objects at load time, for example in the Monobehaviour’s Awake() and Start() methods. Typically we cache the result in a member variable at startup and have its functional code run in the Update() method.

In the following article we’ll add a Spawn Manager to coordinate our Enemy Spawns and we’ll start making use of the GameObject.Find() method:

Always remember to do a null-check.

On some occasions we will have to use a variant of the GetComponent() method by accessing the Component by name instead of by Type but it’s a good practice to avoid using GetComponent with a string for performance reasons.

Tip: Component.GetComponent will return the first component that is found without any particular order. If you expect there to be more than one component of the same type, use Component.GetComponents instead, and cycle through the returned components by fetching a unique property.

In this article we’ve been introduced to Unity’s Component system and how to interact with them through scripting. In the following article we’ll have a look at implementing this logic by adding a Spawn Manager that handles our enemies, drops, power-ups, etc.

Previous | Next

--

--

Gert Coppens
Nerd For Tech

Software Engineer — Unity Game and Application Developer