Unity3D: Script Communication in Unity using GetComponent

This tutorial covers how to reference methods from script components of other game objects.

Within a Unity project, there are several game objects that interact with one another. Their behavior can be programmed through a combination of enabling triggers and writing scripts that execute upon those triggers.

In the previous tutorial, triggers were enabled for the collider components within each of the gameObjects: Player, Laser, and Enemy. This allows the gameObjects to react through the use of OnTriggerEnter in our Enemy script component.

Figure 1: OnTriggerEnter script

We are able to run simple commands like Destroy() to affect either the other.gameObject or this.gameObject. However, what if there is a method from another game object’s script component that needs to run upon the OnTriggerEnter()?

Objective: We will explore how to execute methods located within other components by creating a method within the Player script component. This method will subtract 1 life whenever the Player is hit by an Enemy game object. The method will be our damage to the Player, but it will be called within our Enemy script.

First, our player needs 3 lives. An integer variable, _lives, will be created within the player script.

Next, a new public method will be made called Damage(). It will be public because we want other methods outside this script’s gameObject to reference it. This method will subtract 1 life whenever it runs and will destroy the Player gameObject when _lives becomes 0.

With the Damage() method created within the Player script component, it will be added to the OnTriggerEvent() within the Enemy script component. As seen in Figure 1 above, we have logic for when the Player collides with the Enemy and when the Laser collides with the Enemy. We want Damage() to be included within the if statement for the Player collision.

In other words: If the Enemy gameObject is triggered when the Player gameObject enters, run Damage() to subtract one live from the Player.

To retrieve Damage() from the Player script component, we use other.transform.GetComponent<[Name of Component]>. This command references the other gameObject and its transform — everything within the gameObject inside the hierarchy. GetComponent will retrieve any component within the gameObject, i.e. Mesh Renderer, Materials, and scripts components. We want to retrieve the Player script component and will follow that by calling out Damage().

This can be left as is. However, if there was ever a possibility that somehow the Player script component was deleted and the game tried to call method from that component, the game itself will crash since the component would be considered null.

To avoid crashing the game or future games, it is good practice to only run the method if its referenced component is not null. This will be done by assigning the other.transform.GetComponent<Player>() to a new variable within the Enemy script component and by including an if statement.

As seen above, a new player variable will be created that references the Player script component from the other gameObject.

Next is the if statement: if the player component is not null, then proceed with running the Damage() method from player.

Altogether, it is written within the Enemy’s OnTriggerEnter method as such.

When we run our script, our Player gameObject will be destroyed if the Enemy gameObjects collide with it three times.

If you look at the Lives under the Player script component on the bottom right, you can see the number of lives countdown from 3 to 0.

--

--

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