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.
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.
- Player
- Enemy
- Laser
- Create a script for each of these objects.
- Give each object a Collider component and check the “Is trigger” box. We can give the enemy object a Rigidbody. as well, and set the gravity factor to zero.
- Make sure you can move the player, fire lasers and have the enemy descend fromD#he top of the screen to the bottom in a straight path. (you can find my tutorials on these in my previous articles) Simple Player Movement in Unity 2D, Instantiating & Destroying Game Objects in Unity, Introduction to Physics in Unity, OnCollisionEnter Vs. OnTriggerEnter — When to Use Them?
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.
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.
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 “Player” tag 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.
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.