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 www.jeannoelseneque.com

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

Recommended from Medium

Group Functionality In TestNG

Functional Decomposition: Modularisation by Business Functions — Not Technical Layers

Remote Kubernetes Development in Visual Studio Code with Okteto

Synology Disk Station 1019+ 5-Bay NAS System REVIEW | Mac Sources

XSS made easy for testers, developers and managers

The Art of Creating Robust Mathematical Programming Models

Relation to the Community (Week 8–9)

Feature engineering for Date & Time — Python

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 www.jeannoelseneque.com

More from Medium

How to Install the Universal Render Pipeline

Adding Post Processing to our Prototype

Working with Unity’s 2D Tilemaps

An overview of the Unity Hub editor