Enemy Creation Part3: Script Communication in Unity using GetComponent

Joe

--

Now that the player can shoot a laser, which when it collides with the enemy destroys both the laser and the enemy thus removing them from the game. The next step is to look at what happens when the enemy collides with the player.

It would be a rather unforgiving and frustrating game if as soon as the player was hit it was Game Over!!! So lets introduce the concept of player lives. To do this first create a private variable for the lives attribute. This can be serialized so that it can be modified later in the Unity inspector.

Create Lives attribute

The _lives variable was created as private because it belongs to the player and only the player script should have direct access to it. As we want the enemy to be able to tell the player about the collision and that the player has lost a life a new method needs to be created to let this happen. To let the enemy script have access to this new method it must be explicitly made public otherwise it is created as a private method to the Player script. So calling this method Damage it would look like this

Damage method

The Enemy script will be able to call this method and from inside this method we can access the _lives variable. So when this method gets called we need to reduce the Player’s _lives value. To do this we can use

Subtract 1 from _lives value

The next step is to determine if the Player has run out of lives or can still keep playing. This is done using an if statement checking to make sure _lives is still greater than 1. If it is not then it has reached zero or less. At this point we can call the

Destroy the Player

As the script calling Destroy() is the Player script and is attached to the Player gameObject the this.gameObject can be used as this would signify the gameobject which this script is attached to.

Now that the Player has lives which can be decremented and the Player can be destroyed when they run out of lives, how does the Enemy access this Damage method.

This is accomplished by use of Unity’s getComponent<>() method. It is called like this

Accessing the Player Script from the Enemy script

Now lets break this down a bit to understand what is going on. In our Enemy script when we collide with an object it is assigned to the other variable. So when we check the tag of what has been collided with we check other.tag similarly when in the if statement that belongs to the Player other now holds information on the gameObject tagged as Player.

The only item that this has direct access to is the transform. So by saying other.transform we are saying the same as the Player’s transform.

From this we will need to get a component and all the components are visible in the inspector. So using the GetComponent<>() method components like the Mesh Renderer, the Capsule Collider, or the Player script can be accessed. To specify which component that needs to be accessed the name of the component is placed within the T-brackets. The Enemy script needs access to the Player script which is called Player so <Player> does this.

Now if the Player script does not exist then this will cause an issue and the game will crash, if Unity doesn’t when it compiles. To protect from this the GetComponent<>() call can be stored as what is know as a reference variable. This is done simply by assigning it as

Creating a reference variable for the Player component

This will now allow for checking player which is the Player Component. Using an if statement to check to ensure that it is not a null value we can continue. Otherwise instead of crashing the game will continue, although not doing what is intended.

Then the player variable can access the Damage() method from the Player script like so

Access Damage script from player

Now when the game is run the Player will lose lives when an Enemy collides with it. This can be seen in the inspector each time an Enemy collides with the Player by the drop of one life.

Player Lives dropping when hit

When the players lives reach zero then the player is destroyed.

Player destroyed when lives reach zero

--

--