Using Events to Destroy all the Enemies

Proof of Concept I am working on

This article is part of an ongoing series of gems I discover while making a shoot 'em up that I feel is worth sharing.

As the objective states, in this game, I want the Spawn Manager to stop spawning enemies and have all the enemies on the screen explode as soon as the player dies just so it has more of an impact. There is a number of ways that come to mind on how we could tackle this.

  1. Every Update frame — Have each active enemy currently in the level constantly checking if the player is still alive and if not, destroy itself. Have the Spawn Manager do the same.
  2. Game Manager — Holding a list of active enemies and monitors the player and if the player dies, go through its list and destroy each enemy. Have the Spawn Manager do the same.
  3. Events — Have the player be the publisher of an event and have each enemy subscribe to that event. When the player dies, the publisher fires the event and all the subscribers of that event are notified. The key thing is the publisher doesn’t need to know who the subscribers are

As you guess, we are going to implement the third option.

In my player script, I define the event. By standard naming convention, we start it with “On…” followed by what happens. In this case, “OnPlayerDies”.

In the PlayerController script, in the OnTriggerEnter2D method where it detects, if there is a collision with an enemy, I am going to set up the trigger for the OnPlayerDies event by simply calling it.

If we were to play the game now, it would show an error because there are no subscribers for this event yet. So the underlying structure for the event is set to null.

All we need to do is do a null check.

You may not be aware but since C# 6 we can shorten this by calling Invoke. This Invokes the OnPlayerDies event only if the event is not null.

With the event defined and triggered, let’s move on to subscribing to this event. First of our subscribers is the Spawn Manager. First, we need a reference to the PlayerController and then subscribed to the event and specify which function to call when the event is fired.

Notice in Visual Studio, when you typed += if you press TAB, it autocompletes the statement with a function.

Let’s replace the function’s code with disabling the Spawn Manager

That’s it for the Spawn Manager. It will deactivate as soon as the player dies. Let’s do the same for the enemies. Add the same code for the Start method.

In the enemy, I have a method that disables the enemy, has exploding effects, and sends it back into the pool of available enemies (Object Pooling).

It is best practice to also unsubscribe from an Event when you no longer need to listen for it. To do this, instead of the +=, replace it with -= as follows.

Yes, I refactored the code so we get the reference in Awake and subscribe to the event.

A much nicer, cleaner code.

If you enjoyed reading this article give me a Clap, also if you would like to see more, “Follow” me, so you may be notified of future releases. You may also send me a message if you need any further help.

A Data & Analytics Consultant who is expanding into developing experiences in XR, Enterprise and Gaming space using Unity

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

DevOps strategy: How DevOps help boost your productivity?

Coding Craziness Makes For Fun In ‘Quirky Circuits’

The Basic of Front End Development Process

Data models for partial update messages

SqlDBM Joins Snowflake Partner Connect

Web Hosting Fundamentals

[CTCI: Arrays and Strings] 1. Is Unique

Things Learn before using GitHub

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Jean-Noel Seneque

Jean-Noel Seneque

A Data & Analytics Consultant who is expanding into developing experiences in XR, Enterprise and Gaming space using Unity

More from Medium

2.5D Platformer — #13 Rolling

Aiming Features in Cinemachine

Adding Post Processing to our Prototype

2.5D Platformer — Improving the Ledge Grab #10