Tap to unmute

How To Build An Event System in Unity

  • Жарияланды 2023 ж. 24 Нау.

Пікірлер • 494

  • ElegantWaster
    ElegantWaster 3 жыл бұрын +157

    This is really great and very concise. You have quickly become my favourite Game Dev channel on here.

    • Game Dev Guide
      Game Dev Guide  3 жыл бұрын +18

      Wow, thank you! That really means a lot.
      I'm trying hard to strike a good balance between informative and entertaining. I'm presenting a lot of things I think go undeserved by a lot of other channels, and honestly my content for these videos are mostly from things I'm using day to day during development - stuff I wish I'd learned\heard about when I was first getting started. So I'm really glad you're finding it useful! Thanks for the kind words! 😊

  • Seb Ibrahim Kurgunayev
    Seb Ibrahim Kurgunayev 3 жыл бұрын +71

    Your content is amazing. I'm so tired of the same beginner how-to stuff that ends up being a pile of spaghetti code and can't really be used in medium to large projects. This was much needed. Thank you and keep the videos coming! ❤️🙏🏼

    • Maiko
      Maiko 24 күн бұрын

      @Vinton Cerf
      If you want to make sure the right object handles an event, you can assign the event to a delegate in a Dictionary. Then, when the event is triggered, you can specify which object to call the function on using its instance ID. This way, you can make sure that the function is only called on the specific object you want.

    • Vinton Cerf
      Vinton Cerf Ай бұрын

      @2n9 why?

    • 2n9
      2n9 Ай бұрын

      @Vinton Cerf that's not true at all

    • Vinton Cerf
      Vinton Cerf 9 ай бұрын +4

      This system is not performant since he is id checking for every single door in the scene.
      Let's say we have 1000 doors in the scene. The id checking code will run 1000 times whenever a single door is opened. In other words, we say the following to the computer:
      You: "Hey dude can you open this door for me please?"
      PC: Wait pal, first I need to know which door is that what's the id?
      You: The id of the door is 0.
      PC: Ok just wait while I'm comparing that value to all thousand doors.

  • Alex Bükk
    Alex Bükk 2 жыл бұрын +10

    This is the BEST Unity event tutorial I've seen so far! I've been looking around for some practical, example based tutorials for understanding both the general use case and implementation details for event systems. It's a shame I had to watch so many other tutorials before I found this one, that in 5 min covered more than the previous five tutorials I've watched combined. Keep up your good work, and THANKS for this awesome video :)

  • ast*rsk
    ast*rsk 3 жыл бұрын +8

    The quality of this channel is unreal. Keep it up! If you're looking for suggestions on topics, it would be great to get some insight on project planning, scoping, and starting small for new developers (those most likely to find your videos useful).

  • Hunter
    Hunter 3 жыл бұрын +4

    Been learning C# on my own for a year now and so far, your videos have been the best for concise, step by step explanations. A lot of guides don't take the beginner mindset into account, and simply push forward into glossing over and including a lot of extra concepts that end up making things harder to understand. Cheers!

  • Mark
    Mark  Жыл бұрын +2

    One of the clearest Dev tutors on KZclip!
    I love how you anticipate questions and problems, as well as giving a bit of top-level background to the code/unity concepts you are invoking. Top job fella!

  • Caleb
    Caleb 2 жыл бұрын +4

    I like how you explain the concept first. It actually helps to teach us how to code, not just show us what you did.

  • Sevendogtags
    Sevendogtags 3 жыл бұрын +185

    How do you not have like a 100k subs already? Your videos are so nice and high quality!

    • spawayz
      spawayz 3 жыл бұрын


    • João Francisco Biondo
      João Francisco Biondo 3 жыл бұрын +1

      He forgot to remove OnDestroy... Ba dum ts

    • Zak Kaioken 2
      Zak Kaioken 2 3 жыл бұрын

      @Maks Mikhnevych are you forgetting that for someone to subscribe, there has to be enough value in the content? The reason this KZclipr isn't as big is because this is the kind of content you're not going to be looking for to generally get better at unity. this solves a single problem in game development that some or most just don't really have a problem with yet or they have a system they think could be better. you have to be relatively experienced with the engine to want to watch this.
      frequency doesn't have to be in hz. the frequency this channel uploads is 1-2 videos per month. It's good quality content but it's not tackling a problem most have.

    • Maks Mikhnevych
      Maks Mikhnevych 3 жыл бұрын

      Zak Kaioken 2 Why would you try to explain me term introduced in conversation by me? If you want to go meta - the frequency this channel uploads video is about 0,000000578703704 Hz (videos per second) or 3 video each 2 month.
      Just look how rich looks your explanation on topic in one of the last messages. It is nowhere like your original silly point to low upload frequency being the reason of low subscribers count. If you take a guy who uploads more often then it would mean he will get 100k sooner than this channel, which is not true, according to your last improved post here. From our conversation it’s clearly obvious you have matured and became more aware of how things work and rarely will you have a chance to explain complicated process by single line of symbols starting with “That’s because”.

    • Zak Kaioken 2
      Zak Kaioken 2 3 жыл бұрын

      @Maks Mikhnevych Макс Михневич frequency has to do with how often one uploads. if you post every day you will have more content than the person who only posts once every couple months.
      I already gave you the formulae. you remember those / characters in my message those mean divide.

  • Michael Sissons
    Michael Sissons 3 жыл бұрын +6

    Utterly fantastic, most tutorials available on youtube have serious pacing and clarity issues, and yet you made it look effortless.

  • Marcus79
    Marcus79  Жыл бұрын

    It took me a little while to wrap my head around this concept, but as my project grew bigger I really needed something like this! Now that I finally understand it I use it all the time! Thank you for explaining it properly and clearly!!!

  • FenixD
    FenixD 2 жыл бұрын

    A clear and concise video explaining events. I was self-taught with code, so I had been coding for a while before I started to understand events and how to utilise them. Even after learning about them, I still didn't really use them as I had to step out of my comfort zone. Really good explanation and usage to introduce someone to events though. Kudos!

  • Rakowu Music
    Rakowu Music  Жыл бұрын

    That is so damn well explained. Short and sweet but without leaving out a single thing. You answered all my questions in record time. Thank you for the great video.

  • funisfun8
    funisfun8 3 жыл бұрын +3

    This was amazing. I think I need to come back when I'm not so sleep deprived, do a little background research, and try this out for myself to get the full benefit, but I didn't know about this functionality and never thought about programming this way. This is incredible. Thanks!

  • TheMenkin
    TheMenkin  Жыл бұрын

    This has become one of my favorite design philosophies for quick prototyping, and even in some production builds over two years. I've used this for two major game jams, and it has amazingly reduced coding time drastically , although it can be a hard concept to grasp for other devs on short time span. I cannot recommend this tutorial enough. If you have trepidations about it, try it out, it might change your world as much as it did mine.

  • Hero Snowman
    Hero Snowman 3 жыл бұрын

    Your tutorials are so simple and understandable, keep doing this!

  • Xuan Hung Do
    Xuan Hung Do 3 жыл бұрын +8

    Welp, this was one of the most helpful videos for GameDev in unity. Clean, fast and simple tutorial. Great job!

    • GoodUsername
      GoodUsername  Жыл бұрын

      he goes faster than a minecraft dream speedrun but it's very helpful!

  • psinjo
    psinjo 2 жыл бұрын

    I want to thank you for the simple tutorial, it was pretty short, to the point, and showed off the syntax + a decent implementation (along with parameters) keep up the good work!

  • Heather Elroy
    Heather Elroy 10 ай бұрын

    This was great. To the point. Quick typing. No extra fluff. Loooove it.

  • JordanLiver
    JordanLiver 2 жыл бұрын

    I’ve watched this several times and finally pushed myself to use it during a game jam. It worked so well for what I needed! Thanks for the concise explanation and example! It really helped me grow as a programmer.

  • Raghavendra Singh Dasila
    Raghavendra Singh Dasila 3 жыл бұрын

    Really need more content like this which focuses on code and architecture

  • Daniel Rinaldi
    Daniel Rinaldi 2 жыл бұрын +2

    Very nice video as usual. 👍
    Your use case that you show in the video is not the greatest (better to use OnTriggerEnter() and OnTriggerExit() for doors in my opinion). Although, I know that this is just an explanation explaining how to make an event system using an observer pattern and for that it's simple and well edited. Nice job!

  • Gabriel Coleman
    Gabriel Coleman 3 жыл бұрын +88

    Overall great tutorial. One thought, Instead of assigning a Unique ID to each script, an easier way would be to get the gameobject.InstanceID, which would save time of needing to make sure each door has a unique ID.

    • Nachos TV
      Nachos TV  Жыл бұрын

      @Tom Tomkowski Thats pretty lit

    • Tom Tomkowski
      Tom Tomkowski  Жыл бұрын

      @Nachos TV Nah, when passing GameObject as a variable you are passing only the reference to this gameobject. You are not passing all the data and variables that are included io this GameObject. So here, the most useful and easy way would be to pass GameObject.

    • Toafloast
      Toafloast 2 жыл бұрын

      You can also check if the value is 0 when the game starts, or in other words, you didn't already change it. If you didn't change it, assign its unique object id.

    • Luuk Domhof
      Luuk Domhof 2 жыл бұрын +6

      @Nachos TV When you send a GameObject (Or any variable for that matter) as a parameter to a method, it's only sending the pointer to the memory location, not the actual data. So it doesn't matter what type of data you send as parameters. At least not locally.
      There are slight differences in garbage collection and storage size for initialized variables. But nothing noticeable at such a scale. And since you're only sending a reference of the GameObject, you're not initializing anything anyway, so it's all good.

    • Des
      Des 2 жыл бұрын +22

      @Nachos TV I'm no expert either - but I imagine that these are reference types and you would't actually be passing the entire GameObject

  • Etocles64
    Etocles64  Жыл бұрын

    Wow, what a clear, concise, and informative video. Absolutely amazing work, thank you!!!

  • Derek Gooding
    Derek Gooding 3 жыл бұрын +1

    This stuff is gold! I've watched a handful of your videos now and I'm astounded by how useful they are.
    Please keep up the good work.

  • Rick Loyd
    Rick Loyd 2 жыл бұрын +2

    After a week, I came back here to say THANK YOU! This pattern is awesome, I just used it in an upcoming project, everything looks easy to understand, clean and safe!

  • OXMOND Tech
    OXMOND Tech 3 жыл бұрын +1

    Great tutorial! Accurate and clear description and great code. Well done! 👍🤓

  • MilkyLyrics
    MilkyLyrics 2 жыл бұрын +1

    im a CS master student who was always quite interested in game dev but in my bachelor and master we havent learned so much about game dev and looking at unity, i always felt somewhat overwhelmed with all the custom functions in unity. Just found your channel today and it gives me a nice starting point to at least see all the options in unity

  • Cammin
    Cammin 2 жыл бұрын

    Another step to go further with this is using scriptable objects to make things greatly modular. Unity has 2 nice videos about it

  • Roderick Morgan
    Roderick Morgan 3 жыл бұрын +10

    I've got to echo all the comments below, this is one of the quickest, clearest and most aesthetically pleasing dev tutorials I've seen on youtube.

  • Tsvetan Sumerski
    Tsvetan Sumerski  Жыл бұрын +1

    I've been watching videos on events for days and this is the only video that actually explained it well enough for me to understand, thank you!

  • L
    L 2 жыл бұрын

    I'm having trouble digesting all this about patterns, and now you just made a key subject easy to understand, I really appreciate that.

  • varun shivaram
    varun shivaram 3 жыл бұрын

    Keep making amazing videos like this to explain kinda complex stuff simple...as an aspiring game dev I loved the video...you deserve more subs, I'll make sure to like,sub, refer, and tweet...keep up the good work.

  • JDoerp
    JDoerp 2 жыл бұрын

    I’ve been trying to achieve exactly what this video has explained for four days and not been able to until now. Earned a subscriber. First time I’ve come across your channel. Thank you!

  • Nemlinka Company
    Nemlinka Company 3 жыл бұрын +1

    I love your content.
    Fast + clean + VERY usefull.... I never saw channel like this (most are too unprepared)
    I really wish you success

  • Helios Games
    Helios Games 2 жыл бұрын

    Awesome video thank you!
    I've been wanting to make a much bigger event system which can be used on any sort of events, something like delegates in Unreal. This is a good starting point :)

  • Josh Owens
    Josh Owens 2 жыл бұрын +1

    This is a great tutorial - though I typically use UnityEvents over C# events because of the easy integration with the editor

  • Brief
    Brief  Жыл бұрын

    Thank you for the video. Really helpful. Question: What are pros and cons of Event System as a separate class with all the events vs events scattered around in scripts?

  • Joaquín Frausto Alfaro Rocco
    Joaquín Frausto Alfaro Rocco  Жыл бұрын

    let me tell you, this just leveled up my overall coding pipeline, simple, concise a work of art this tutorial

  • Ard Fantasy
    Ard Fantasy  Жыл бұрын

    Thanks for this video, I have seen a lot of event systems that didn't implement it with parameters, this was the first one that did! This really makes things a lot easier :)

  • Kartik Tiwari
    Kartik Tiwari 3 жыл бұрын +14

    The event system was doing my head in. But you solved it in 8 minutes amazing stuff!

  • Drake l
    Drake l 3 жыл бұрын +11

    Great video! Subscribed. One question: what is the difference between doing it this way and using UnityEvent and UnityAction in the UnityEngine.Events namespace?

  • Antman261
    Antman261 2 ай бұрын

    THANK YOU! I come from a web dev background and build a lot of event-driven and/or event-sourced distributed systems. I've just started building a game as a hobby. SO, naturally, I decided to punish myself early and went looking for an event-based implementations in Unity. The first few forum posts and packages I came across made me doubt the entire approach, but this is precisely the kind of clean, simple, straightforward thing I LOVE! Thank you!

  • Bloodthirst
    Bloodthirst 3 жыл бұрын

    One other way that i manage events is to encapsulate them in a GameEvent scriptableObjects , then i create a GameEventListener script where i create a GameEvent field i can plug my event SO , and a UnityAction field for the listeners , i find that waay more flexible and more editor friendly. Good content man , this is helping many people get their code cleaner ! :)

    • Game Dev Guide
      Game Dev Guide  3 жыл бұрын +1

      Yeah, this is another great way to handle it, especially if you're working in the editor often or want designers to be able to set up relationships easily.
      I personally find it a bit messy, as it can be a lot harder to debug and understand the relationships at a glance. But there's never one right way to do something! Just whatever is best or easiest for the project at hand!
      Glad you enjoyed the video!

  • magnusm4
    magnusm4 3 жыл бұрын

    About Unity Events not depending on each other.
    I heard the same thing about Scriptable Objects, so that if you delete or disable the player or an enemy then your inventory doesn't go bonkers cause the player holds the code and variables to the inventory. Instead it's put in a Scriptable Object that's open to everyone to read from, making it much more flexible and easier to access without getting any errors while changing or testing new things

  • Simon Codrington
    Simon Codrington 2 жыл бұрын +1

    What a great video mate. Love how you've animated it to keep it interesting. Cheers

  • Jack Peterson
    Jack Peterson 3 жыл бұрын +2

    This is high quality content! You cover topic that's actually important. Thank you

  • Serj_12
    Serj_12 2 жыл бұрын

    Hi Matt!
    I know it passed a while since you uploaded this, but I just knew your channel a week ago! And Im addicted now, I love this approach for non fully beginners, but still "followable" since all code is there.
    With regards to this video, let me ask you a question, implementing this has any impact on performance or is it more focused on clean and ordered code. In a project in an advanced state would be worth the effort?
    Thanks for the videos!

  • 08 - Aditya Kothari
    08 - Aditya Kothari 6 ай бұрын

    Fantastic tutorial, keep up the great videos!

  • Mahima Gupta
    Mahima Gupta 6 ай бұрын

    It's truly astounding how easy the internet makes learning these days. Thanks for the tutorial, my guy.

  • Ricardo Ferreira da Silva
    Ricardo Ferreira da Silva 2 жыл бұрын

    Thanks for the video! I found this pattern extremely helpful for triggering changes in the UI. Its very boring and messy to make direct connections between logic components and UI components. Using events you can just fire away upon input and let the needed components listen to it and do what they need to do.

  • rene rodriguez
    rene rodriguez 3 жыл бұрын

    This is great, sounds like combining Mediator pattern with Observer using Events and Singleton. Nice

  • Jannis Kappertz
    Jannis Kappertz 2 жыл бұрын +2

    Hey, cool tutorial. Thanks.
    Small question: Wouldn't it make more sense to make the whole GameEvents class a static class. It doesn't make much of a difference, I think. But maybe you have a reason for not using static classes?

  • Lando
    Lando 3 жыл бұрын +1

    love the video format! I appreciate you speeding up the coding sections and highlighting actual gameplay functionality.

  • Rick Loyd
    Rick Loyd 3 жыл бұрын

    Even thought I am using singletons for years and events as well, I never thought to use singleton as an intermediate system between two codes. Awesome tip, thank you so much and I hope one day you will resume working on your channel!

  • Karim
    Karim 3 жыл бұрын +9

    Hi, cool video, but I recommend putting the subscribe/unsubscribe lines of code in the Enable/Disable methods, because unless you call DestroyImmediate you don't really know when the object is destroyed.

  • Smoochiekins
    Smoochiekins 2 жыл бұрын +9

    Best practice in Unity is to handle event subscribes / unsubscribes in OnEnable and OnDisable, since those are invoked when Start and OnDestroy is called anyway, but it also allows you to cleanly enable and disable the game objects.

    • God Mode
      God Mode 9 ай бұрын

      I dont agree because a disabled object should be able to respond to events.

  • Indie Progress
    Indie Progress 2 жыл бұрын

    Incredible guide. Thanks, man!

  • Ponkadev
    Ponkadev 3 жыл бұрын

    This really made the way event systems work a lot more clear for me. Thanks a lot, hope you have a good day!

  • Nanjaare
    Nanjaare 6 ай бұрын

    Great job ❤️ Big thanks!

  • Jagent
    Jagent 3 жыл бұрын

    Never before have I needed knowledge so badly without realizing it. Thank you for this!

  • Yanan Liu
    Yanan Liu 2 жыл бұрын +1

    Hello, I have a silly question. The OnDoorWayOpen(int id) function will be called n times (n = the current door numbers) when only one is the right one. Isn't that considered low performance compared to directly save the door's gameObject reference on the trigger area component? I wonder when should I use the event-system and when should I directly save the reference.
    I also wanna ask how to set the Func returnEvents. :3 I am also working on solving it by myself.
    Thanks! Awesome video btw!

  • Iván Moreno
    Iván Moreno 3 жыл бұрын

    Thanks a lot! I was having an issue with Unity crashing when some events happened, so I watched this video to check what was wrong with my code. Your example worked perfectly.
    Later I realized that the problem was an infinite recursion lol

  • Nick Olas
    Nick Olas  Жыл бұрын

    Finally someone that explains this clearly! Many thankd👍👍

  • Hannah
    Hannah 2 жыл бұрын

    Could the event system also be used to update UI scores if a player does a certain action? Or is this only used for handling collider actions?

  • Pedro Paulo
    Pedro Paulo  Жыл бұрын

    This is really amazing. Thank you!

  • martin faucheux
    martin faucheux 2 жыл бұрын

    Great and clear! Thanks a lot!

  • _bujka
    _bujka 2 жыл бұрын

    Thanks for your work! Super helpful.

  • Alice pricesa ei bi
    Alice pricesa ei bi 6 ай бұрын

    As for me, everything is simple and clear. Thank you very much

  • Zach Cantwell
    Zach Cantwell 2 жыл бұрын

    I love your vids man; you're an awesome teacher.

    MAUCO  Жыл бұрын

    Maybe I am a bit late to thank you, but your video saved me hours of suffering... Thanks a lot! Keep up your good work!

  • Darius Sparkes
    Darius Sparkes 2 жыл бұрын

    A brilliantly constructed guide.

  • Fluffy Kaeloky
    Fluffy Kaeloky 3 жыл бұрын +1

    You can use the UnityEvent class to benefit from automatic Unity serialization, as well as null protection if your GameObject was destroyed.

  • zakaruPL
    zakaruPL 3 жыл бұрын

    Great quality of video! keep your work doing mate 🤜🤛

  • Jasonario
    Jasonario 3 жыл бұрын

    This was a great video! Adding this to my toolbox for sure!

  • Artyom Volkov
    Artyom Volkov 3 жыл бұрын

    I used to use a C# events before I learned about Unity events, but your video gives a new look on them. Thank you.

  • Hannah
    Hannah 3 жыл бұрын

    Would this be added along with a game manager? If so is it any different how the event system notifies the game manager compared to other classes since the game manager is also a singleton? I would really love to implement the event system in the future!

  • Mike Arcuri
    Mike Arcuri 2 жыл бұрын

    This was great! My computer science education is 25 years old, and I'm pretty new to C#, but I was able to follow this lesson the first time, get it all working, and understand the basics.
    It wasn't clear to me why we needed both the public methods and the public events defined in the GameEvents object, but a few edits to try to call the events directly from the TriggerArea objects showed me via C# errors that this isn't allowed.

  • Titik Universe
    Titik Universe 2 жыл бұрын

    Очень круто! Спасибо

  • shippous
    shippous 3 жыл бұрын

    Incredible tutorial. Thanks!

  • Jaypordy
    Jaypordy 3 жыл бұрын

    Are there drawbacks of using too many events? Also should I make this event class a singleton as well (with DoNotDestroyOnLoad()), if there are multiple scenes in the game?

  • simoxeh
    simoxeh  Жыл бұрын

    I've been trying forever to understand events. I finally think I understand it. May you have many blessed days and may your descents know nothing but happiness

  • Tomáš Ulip
    Tomáš Ulip 3 жыл бұрын +1

    Very good video. I've been struggling with almost the same problem for quite some time and I've come up with exactly the same solution.
    I wonder, is there any way to get rid of the ifs with ids? My idea is to listen only to the event of one particular door you are close with because if you had too many doors it doesn't make any sense to trigger the event for all of them every time player comes near. But I have hit the same problem - the event is the same for all of the doors so they all open... any ideas?
    Opening of door isn't of utmost importance to me but I have many units with hp in my game which if goes

  • C
    C  Жыл бұрын

    This was super helpful, thank you so much!!

  • TrueIndian
    TrueIndian 3 жыл бұрын

    All your videos are really good.Keep up the good work.

  • Hamza Zahid
    Hamza Zahid 6 ай бұрын


  • Sufian Ahmad
    Sufian Ahmad 2 жыл бұрын

    After 7 or 8 videos I finally got a focused video 😊 Thank you

  • David Zapata R
    David Zapata R 3 жыл бұрын

    I love your channel its so useful 🤩

  • Noel Abella
    Noel Abella 3 жыл бұрын +1

    Heaps of thanks, I figure it out! Please make more of these! This helps me in my assignment.

  • ManuGamingCreations
    ManuGamingCreations  Жыл бұрын

    Thanks a lot! Managed to make a similar thing in Javascript thanks to this tutorial!

  • magnusm4
    magnusm4 3 жыл бұрын

    What's the biggest difference to using this over regular delegates?
    I only found an answer that Unity Events have restrictions and used for the editor to serialize the events

  • HasanRakib
    HasanRakib 3 жыл бұрын

    Does this mean i have to make separate script/class for each listener if they each look for different events ? is there a way to create one listener class that can be used to select different events in the inspector (maybe through an enum) from the "GameEvent" class ?

  • TryHardStudio
    TryHardStudio 2 жыл бұрын

    So useful, thank you very much!!

  • T N
    T N 2 жыл бұрын

    What do you use for your presentation building? It's so nice.

  • Seraaron Art
    Seraaron Art 2 жыл бұрын +1

    I'm new to Unity, but I'm watching lots of different tutorials and going through an osmosis phase in my learning. Can someone explain the difference between doing this vs using scriptable objects? Thanks!

    • Kaleb _T
      Kaleb _T 2 жыл бұрын

      scriptable objects are a completely different thing , to my knowledge

  • Hernando Nieto
    Hernando Nieto 2 жыл бұрын

    It would be great if you could make a tutorial about Multiplayer games using Photon for Android platform. Thanks!

  • Postie
    Postie 3 жыл бұрын +1

    I've got a really dumb question. For a provided example, wouldn't it be simpler to handle door functionality locally in a door-specific script? Alongside a trigger maybe.

  • Paweł Horaczy
    Paweł Horaczy 2 жыл бұрын

    Thanks, you explained everything well :)

  • K i n g
    K i n g 5 ай бұрын

    This was so helpful!! Thank you

  • Videogueimeiro
    Videogueimeiro 2 жыл бұрын

    Thank you for this video! You solved my problem in 8 minutes :)

  • Sree R One
    Sree R One 3 жыл бұрын

    Hey Man, thanks for Sharing the knowledge and supporting the community.. Cheers!