- Home /
A lack of basic understanding of events and such
Beginner dev here trying to get my head around Events, Delegates, UnityEvents, other stuff like this. I have some questions, but I'm so green I'm not even sure if I'm asking the right ones, so please bear with me.
I had a look at this quick tutorial about static events: https://www.youtube.com/watch?time_continue=393&v=k4JlFxPcqlg&feature=emb_logo
It all made sense and like a few I've seen, the instructor uses the OnGUI method. I think I've read that OnGUI() runs in a continuous loop like Update() does, is that correct?
Basically I've been wondering if there is a standard way in Unity to fire off an event. If so, does it always involve some kind of looping section of code?
eg. A player's race car crosses the finish line and that initiates A,B and C to happen.
Does there have to be a looping code constantly checking if the player's car has crossed that line yet? NOPE-NOPE-NOPE-NOPE-NOPE-NOPE-NOPE-NOPE-YES!
Or can you write it so that when the car crosses the line, there's a one-time only event that gets triggered?
Pretty basic stuff no doubt, any clarification is welcomed and appreciated, cheers!
Answer by unity_ek98vnTRplGj8Q · May 06, 2020 at 09:11 PM
I'm not an expert, but this is my understanding of how events are used and why they are beneficial:
First, to answer your question - no you don't have to use looping code yourself to fire an event but there will always be some underlying loop that is ultimately responsible. Let me give you 3 common ways of firing an event:
1. In an update function or coroutine - this is your 'looping code' where some set of conditions are checked repeatedly until they are true, then you fire the event
2. On a timer - Using Invoke() or something similar, while you are not executing your logic on repeat Unity is ultimately looping through its own logic to check when it is ready to call the method you gave to invoke
3. Based on another event - For example you can use OnTriggerEnter() in a racing game to detect when a car hits the finish line, however ultimately this event is triggered by Unity's looping physics engine
It doesn't really matter how you fire an event, checking a set of conditions every frame is generally pretty performant and standard (depending on your set of conditions). Events are used for organizational purposes NOT for performance gains.
To illustrate the benefits of events, picture a building with a fire alarm lever and a few residents. The 'normal' way of coding would be to tell each resident that the way they can know if there is a fire is to go look at the fire alarm lever every few minutes to see if it has been pulled, in other words each person is responsible for checking if something has happened. Obviously the better way is to tell the residents to listen for the fire alarm bell (register to listen to an event), and then have the fire alarm system communicate the event of the fire to everyone who is listening all at once. This makes things much more organized. Additionally this has the benefit of making things very scalable. If you want to add more residents to the building, you just need to tell them once to listen for the fire alarm. You can also add more fire alarm levers around the building, and all they need to do is trigger the alarm bell when they are pulled.
Sorry for lengthy explanation but hopefully this was helpful for you
Your answer
Follow this Question
Related Questions
Where is the best place to use delegate call back without calling it in Update function? 1 Answer
Setting up EventTrigger programmatically for GameObject throws NullReferenceException 0 Answers
Argument 1: cannot convert from 'method group' to 'UnityAction' 1 Answer
How do I move a Function up or down in the inspector of a Unity Event? 0 Answers
IPointerClickHandler works until I add IPointerDownHandler 0 Answers