Script Communication in Unity

Simon Leen
4 min readMay 17, 2021

--

Objective: Explain what GetComponent is in Unity and explain how game components communicate with each other.

In Unity most game objects will have a script attached to it. These scripts are was allow game logic, settings and variables to be create and assigned. Along with this the scripts have something that allows game objects to communicate with each others scripts. This is GetComponent.

GetComponent allows one script to call to another script using the component or game object. As discussed in the previous post Unity provides functions for physics and collisions using OnTriggerEnter, so we’ll use that to explain further.

If our enemy target has the OnTriggerEnter set in the game object script, we can check when the player and enemy collide or passthrough each other in this case. Using the OnTriggerEnter we can identify the other gameobject that has collided using the game object collider.

OnTriggerEnter from the Enemy script

Within the above function we can check what the other game object was that collided using the tag function. Tags are assigned through each game object using the inspector.

Player game object tagged as Player

Using this tag we can check if the other game object was the Player or any other tagged game object.

Check if other game object is Player

The OnTriggerEnter and if other check are within the Enemy script. Inside the Player script we have a method for taking lives away from the player when the method is called. Obviously when the game starts the player will have been assigned a private variable for the Players lives.

Player lives set

To reduce these lives on collisions or after being his we create a method that can be called when to reduce the lives. This method needs to be specifically set as public and not the default private to allow other scripts to call it.

Damage player method

The above _lives — ; means reduce the _lives variable by 1. There are other ways this can be written like _lives = _lives -1 and on so but I personally find this way to be cleaner.

Back to the Enemy script and accessing the Player through the other.tag. Using the other.tag on its own you can only directly access the other.transform. To be able to interact with the rest of the attributes or methods, you need to use the GetComponent method.

Calling the Damage method on the Player script using the Enemy Script

In the above example, we check if the other game object has the Player tag. If it has then get the other transforms component. We access the Player component by calling it inside the T-Brackers( < > ). From here we have access to the Player component where we call the Damage method reducing the _lives by 1 each time the OnTriggerEnter is called because of a collision between an Enemy and the Player. Using the GetComponent you can get more than just the Players methods. You could call the MeshRenderer and change the material color and so on.

For best practice you should create a reference variable of the Player. This is done by creating a variable player of type Player and assigning the other.transform.GetComponent. Then null check to make sure there is an actual Player component so you don’t end up with errors. Once the null check passed you can then call the Player Damage method. By doing this it reduces the chances of errors and game crashes for your end users.

Null checking for a Player component before calling the Player Damage method

In the next post, we will look at Coroutines in Unity.

--

--

No responses yet