Adventures in GameDev with GameDevHQ! Day 15: Script Communication in Unity with GetComponent.
It may sometimes feel like there’s an Ernestine making it difficult for your scripts to communicate to one another when you’re starting to program in Unity C#, but there’s a very simple way to pass variables and methods between them using GetComponent!
In Unity, we can only gain access to a GameObjects transform by default. If we want to gain other components like another GameObjects script, we’ll need to use GetComponent. For this example, we’ll continue using our Galaxy Shooter game. Our enemy cube already has a box collider set to trigger on it, so if it runs into our player, we’ll want our player to lose a life. How do we do this?
First of all, we need to introduce the concept of player lives inside of our player script so we’ll create a variable.
Next, we’ll create a public method that the enemy will access that damages the player. Remember that public methods can be accessed by anyone, if it were set to private, only the player could access the function and if the enemy tried, we’de get an error because trying to access a private script is forbidden. That said, since the enemy can’t directly change any variables in our player script, we’ll have the player do it themselves.
_playerLives-.- is the equivalent of: _playerLives -= 1 or
_playerLives = _playerLives-1. They all do the same thing and remove 1 from the variable _playerLives. Easy enough!
Next there’s a check to see if _playerLives have run out by checking to see if it’s less than 1. If it is, then destroy the game object for good!
Now that we have the Damage function set up, we’ll use the GetComponent method to directly access it from the Enemy script in its OnTriggerEnter check:
But first, let’s talk about tags. Tags are a way to identify one or more GameObjects with a reference word to make identification in your code a little easier. You can tag your player with a Player tag. Enemies with an Enemy tag, a laser with a Laser tag, etc. Why is this useful? You can differentiate actions depending on who you collide with as we’ll see with this enemy code.
As we can see, when an object collides with the enemy, OnTriggerEnter will run the code and it will check to see if the other object has a Laser or Player tag using the CompareTag method. if it’s a laser tag, it will destroy both the laser (other.gameObject) and itself. (this.gameObject)
But if the tag is “Player”, then it temporarily borrows the players script and runs its Damage() routine! It does this using the GetComponent method by first creating a reference to the player inside of its own script:
the capital Player is the object type, showing that it’s an actual object in the game, and we’re going to create a variable of that type of object. Then, using the equals, we assign the actual collided player into it by using GetComponent:
remember that other is the variable that gets passed into the OnTriggerEnter method. We can really call it anything like target, trigger, BugsBunny, whatever you want! But other makes sense because it’s the other item you’re colliding with.
remember that in unity, we can only access another objects transform directly. If we want anything else, we’ll need GetComponent:
Here is where the magic happens! Now we have direct access to the Player script! Now we can access the Damage method inside of it and this is what the script does:
the if(player !=null) check is to protect the script from encountering a problem that happens often, sometimes you’ll try to access a script and it won’t exist anymore because the player has already blown up, or for whatever reason, it wasn’t accessed. Instead of breaking the program and crashing, if the player script doesn’t exist, it will just ignore the instructions that access it, but if it does exist, then run the Damage script!
Let’s see it in action!
I’ve placed multiple enemies on the screen and if we look at the player when it gets hit with one of them, we’ll see both the enemy disappear and the players own script go down one life until it gets to zero, in which both the enemy and the player disappear!
Tomorrow, we’ll go over Coroutines!