Script Communication in Unity: Using GetComponent

A basic explanation of the GetComponent function and how it is used to make game objects interact with each other.

Raymond Mills
4 min readAug 17, 2023

When you need one game object to interact with another you need to use a function called GetComponent. As an example of this I am going to have an enemy object damage the player object on collision and destroy the enemy object as well.

Preparation

  • To keep it simple, just add a cube for the player game object, add a cube for the enemy game object and add a capsule that we can use as the laser. (You can give each a simple colored material if you wish)
  • Create the following tags and assign them to the appropriate objects in your game.
  1. Player
  2. Enemy
  3. Laser

Let’s Get Started

We are going to damage our player when it collides with our enemy. First, we need to establish what damage is and what it will do to the player. In this case we have a player with three lives and when the player collides with an enemy the “damage” will take one life from the player and the enemy will be destroyed. Let’s start by adding a damage component to the player script.

This says if the player has more than 0 lives, we take one away.

Now we need to trigger this method when an enemy collider connects with the player collider. We will edit the script that controls our enemy behavior and use GetComponent to call up the Damage method from our player script. For this, we need to take a look at our EnemyBehavior script and get our scripts to communicate with each other.

Here PlayerMovement is our player script, which will be accessed when “player” is used in our code. (player.Damage will call the Damage method within the PlayerBehavior script.)

So, this is telling us that when the enemy game object collides with another object (other) OnTriggerEnter2D is activated. (Think of this as an on off switch or “trigger”) If the other object has a tag of “Player” we access the PlayerMovment script and if the object with a “Playertag is present, (or NOT null) we activate the Damage method inside the PlayerMovment script and also destroy the enemy object that made the contact with the player object.

Inside my PlayerBehavior script I have code that says; if Damage causes player lives to equal zero, it will activate the OnPlayerDeath method within my SpawnManager script and destroys the player game object.

When lives equal 0 the player is destroyed and OnPlayerDeath is called from the SpawnManager script.

By making the _spawnManager variable within the player script we can call the OnPlayerDeath method from the SpawnManager script, using GetComponent to open this communication.

Inside the PlayerMovement script the OnPlayerDeath method is called if _lives is equal to zero. By using our variable _spawnManager, we can communicate with our SpawnManager script and activate the OnPlayerDeath method.

Inside the SpawnManager script, we need to write the OnPlayerDeath method. Inside this method we need a switch that we can trigger to turn the SpawnManager off. For this we will make a Boolean type variable called _stopSpawning with a default value of False.

We need to insert this “switch” into our _spawnRoutine method so that this coroutine can be turned off, and back on if needed (Continue?/Retry?) While _stopSpawning is False (its default setting) the _spawnRoutine will continue, and if _stopSpawning is True, the coroutine breaks connection with the while loop successfully stopping the _spawnRoutine from running until _stopSpawning is false once again and start coroutine is called on to restart the loop.

To keep this cycle from repeating we now need to change the value of _stopSpawning to True.

going to check for the presence of the player. If the player is still alive, then we will continue to spawn enemies. If the player is not alive, then _stopSpawning becomes true.

Summary

Now you should have a basic understanding of how the GetComponent function is used and the many ways it can be used.

--

--

Raymond Mills
Raymond Mills

Written by Raymond Mills

Unity Software Engineer, I am in an apprenticeship with GameDevHQ. I am taking the steps to become a professional Unity Developer.

No responses yet