• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
Question by willywarbucks · May 06, 2020 at 03:34 PM · eventevent triggeringevent handlersevent-listener

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!

Comment

People who like this

0 Show 0
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image

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

Comment

People who like this

0 Show 0 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

129 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Setting up EventTrigger programmatically for GameObject throws NullReferenceException 0 Answers

Where is the best place to use delegate call back without calling it in Update function? 1 Answer

Make prefab that generated more than once listener in Unity Events 0 Answers

ArgumentException for UnityEvent 0 Answers

Method name expected when adding Event Trigger using script 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges