Connect with us

Subscribe

Developer Hub

A Short Guide to an Event System in Unity and Singletons

What is an Event System in Unity?

The primary roles of the Unity Event System are as follows:

  • Manage which GameObject is considered selected
  • Control/Manage which Input Module is in use
  • Change/Manage Raycasting (if required)
  • Updating all Input Modules as required

If you’re a Unity developer, you must have heard of singletons, mostly we use them to avoid the pain of referencing MonoBehaviour instances to each and every script we need them in.

Event System: Singletons

So first, let’s take a look at how singletons are used :

SingletonExample.cs

event system - a singleton script

You create a static variable of the type of the class you’re making a singleton of, then in the Start (or Awake) function you set the value of the singleton to be the current script, and this should be the only MonoBehaviour of it’s type in the whole scene.

So let’s say we need another such singleton and each singleton has a public method.

AnotherSingleton.cs

a singleton script

Now to call the two methods, in two different classes, we can use these singletons :

Test.cs

test script test.cs

For smaller projects, this approach is absolutely harmless, but as the project size increases, more and more classes are added, you add more and more features and responsiveness in your game (e.g. play sound when hit, show blood, shake camera etc.), in such a scenario you’ll just keep filling your project with singletons to manage it properly, which will make it even more difficult to manage.

man pouring singletons in his project

Creating Your Own Event System

A way to avoid this is creating your own event system and using it instead of countless singletons. It can be used like a central system to receive messages from all parts of your project and telling everyone who wants to know about those messages.

Player took damage? Just tell the central system that happened, it will tell everyone who’s got something to do with that (every class that has a function to fire when player is hurt).

Let’s see how to create an event system :

GameEvents.cs

eventsystem script

We create a delegate to with proper parameters, it’s just the introduction so I’ve not included any parameter. Then we create an event OnButtonClick which we then raise with the help of RaiseOnButtonClick. It checks if the event is not null and if not then invokes it.

Now we can simply call the raiser function to invoke OnButtonClick event. I shall do that by creating 2 subscriber scripts to demonstrate how Events can be used to perform multiple tasks in multiple classes.

Subscriber1.cs

Subscriber2.cs

The GameEvents.OnButtonClick += Method2 is the syntax to subscribe to the event, while GameEvents.OnButtonClick -= Method2, is the syntax to unsubscribe from the event and it’s a good practice to always unsubscribe from the events in the OnDestroy method to avoid inviting bugs to party in your project.

And then we can change out Test.cs to call the raiser function.

Test.cs

In Conclusion

In this pattern it’s very safe to subscribe and unsubscribe to events in the event system, and it is a very effective way of avoiding too many singletons.

It might seem a bit of a work at the start but once you get the hang of it, you won’t have a project without a central Event-System.

I’ll keep posting about useful things to learn and and short guides in future, follow me on twitter (@BuluSama) to stay updated about my next YouTube tutorial.

I hope you learnt something new from this post and will start using event systems.

Have a nice and productive day!!!

Want to contribute to TechLater.com?

We have a number of indie developers writing for TechLater.com, and we’d love to provide you with a platform to chronicle your journey – be it game development, coding, designing, ethical hacking or any other computer science.

It’s easy. Simply register here, and start writing!

Newsletter Signup

Get notified of new articles and have fresh tech news delivered straight to your inbox.

Written By

I'm a freelance Game developer, web developer and AI enthusiast I love working on VR projects and simulations. I love writing fiction, watching anime and playing games in my free time, which I rarely have any. Reach me out for freelance work on https://www.fiverr.com/lazydevofficial. follow me on twitter : https://twitter.com/BuluSama

3 Comments

3 Comments

    You must be logged in to post a comment Login

    Leave a Reply

    TechLater: Chasing XP Podcast

    Chasing XP Podcast

    TechLater presents: The Chasing XP Podcast.
    Developer interviews, industry chat, indie development discussion and more.
    Listen now!

    FOLLOW US

    https://twitter.com/Tech_Later

    FIND US ON FACEBOOK

    https://www.facebook.com/techlater/

    More From The Developer Hub

    Write for TechLater.com

    We have a number of indie developers writing for TechLater.com, and we’d love to provide you with a platform to chronicle your journey – be it game development, coding, designing, ethical hacking or any other computer science.

    It’s easy. Simply register here, and start writing!

    Newsletter Signup

    Get notified of new articles and have fresh tech news delivered straight to your inbox.

    | Copyright © 2020 TechLater.com | Powered By Coffee and Kangaroo Burgers |

    Connect
    Newsletter Signup

    Get notified of new articles and have fresh tech news delivered straight to your inbox.

    TechLater