Space Shooter 2D — Day 6

Michael Hatfield
5 min readApr 14, 2022

Communication Between Scripts in Unity

Adding a damage system to the player.

“HEY! What’cha doing over there??”

Development on the Space Shooter 2D game is progressing quite nicely. I have player movement, firing lasers, and enemy movement in place and working. Today I am writing the code to have these game objects interact with each other in a meaningful way. After all, the game is called “Space SHOOTER 2D” not “Do Nothing But Fly Around Littering Space With Little With Pellets 2D’.

Here is the todo list for today:

  • Enemy collides with something — test to see what it was.
  • If it was a laser bolt, then destroy the enemy and the laser bolt.
  • If it collides with the player, then destroy the enemy and damage the player in some way.

It is important to note that the order of events in your code matters. We cannot destroy the enemy game object and then do something else in that script because when it gets removed from the game using Destroy(gameObject), the script gets removed as well, so no more code from that script gets executed.

Okay, seems easy enough. Here is the code I have so far and how it is working.

Great! It seems to be doing what we want! I see a problem however… Did you notice that the laser bolt just keeps going after destroying the enemy ship? I need to tell the script to destroy the laser bolt game object as well. Since the information for the game object is stored in ‘other’, I can just use that. But notice that I have to do it before destroying the enemy game object.

Great! Now if a laser hits an enemy ship, both game objects are destroyed! That’s awesome and all, but what about player damage from collisions with the enemy? Let’s create a health system for the player. Maybe start with 3 lives and take one away each time the player is damaged? That sounds reasonable.

If we plan it correctly, we can just create the player damage routine one time and use the same routine later when we add lasers to the enemy ships. It makes sense then to add this bit of code to the player script and access it from what ever game object is doing the damaging.

What? You didn’t think they’d be shooting back? What fun would that be??

The problem is that we need to tell the player game object that it has been hit by something. That will be a little trickier than what we did to destroy the laser bolt. So…..

Time to learn how to communicate between scripts. :-)

First, I created the method to damage the player. This is in the player script. The public keyword allows the method to be accessed from outside the player script. Doing it this way, I can have ANY other game script damage the player. Hey, space is a dangerous place.

The default I set was _lives = 3. Each time the player is damaged, they lose a life. Once the _lives amount reaches zero, the player dies. (The fancy explosions will be added later.)

Now all we have to do is call the method from the section of the enemy script where we checked for the collision:

We know it was the player that we hit so we can use GetComponent<> to connect to the Player script and access the public method we created called Damage(). This allows us to communicate between the scripts!

Lets see how this is working so far:

Perfect!

Okay then. Now let’s add in some error checking so that if something goes when the enemy script tries to communicate with the player script, we can handle it gracefully. This is always a good idea to do and can save you some headaches in the long run.

So instead of directly accessing the player script from the player game object, you can store it in a variable and then check to see it it worked. If the player script were missing for some odd reason (maybe your dog ate it) then checking the outcome of storing it would let you know.

If the _player is NOT null (meaning that it does have the reference in it) we can then call the Damage() method.

Again, this is good practice and can save you a lot of troubleshooting efforts if something does go wrong.

So far so good! The next item on the todo list, for Day 9, is to create an enemy spawner that sends in a new enemy every few seconds or so. This will involve learning about Coroutines. After that, in Day 10, I’ll cover how to spawn objects into the game without cluttering up the hierarchy window.

See you there!

--

--