Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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
1
Question by Rphysx · Apr 11, 2014 at 05:25 PM · eventsfunction updateevent handlers

Events vs Normal Functions

Doesn't matter how much I strive to understand the usefullness of events, I simply can't see how they differs from normal Functions.

Let's suppose that I need to check if a ball Object has or not touched the floor. Without using events, I'd check at every update() if the ball position in y-axys is the same as the floor. If it is, a function will be called in which something happens.

So what are events useful for ? To do the same with events I need to create-an-istance-of-a-delegate-and-pass-it-in-the-declaration-of-an-event-that-will-then-define-a-method-which-will-be-triggered-if-something-in-different-assembly-as-been-subscribed-to-the-said-event-declaration-to-trigger-the-function-in-the-other-assembly-whaaat ?!

I'd need to do all of this to actually change the function in Update() with an event ? Or pheraps i'm completely wrong and events may be triggered in this scenario even without the use of the update() function ? I mean i'd see the great use of events if in this case for example it would ignite without having to check everytime the y-axys of both objects but it is not the case (as far as I understood)

I find my self always relying to update() if I need to do particular behaviour in case somethings happens, is it wrong ? Because generally I end up checking for loads of information alltogheter in the same frame to understand if I should raise some methods or not, is this wrong ?

What part of events I'm not understanding right ?

Comment
Add comment
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
3
Best Answer

Answer by robertbu · Apr 11, 2014 at 06:02 PM

I don't understand how you are trying to use events. Here's how I might use it in Unity. Imagine a game where a team scored when the ball goes into a goal. The goal could fire of a 'score' event. Different scripts on different objects would receive this event. Other than to make sure not to cause a null reference, the goal does not need to know what objects need this event. The scoreboard would get the event and increment the score. The ball would get this event and return to the starting position. The audio manager would get this event and play a sound. The players get this event and return to their starting position...etc. Only the goal understands the criteria for a score. Each individual script/object handles its own behavior when a score is made.

Without events, the code would be uglier and less-maintainable. You might have a slow, send message solution, or you might have to statically define the list of scripts that need to be informed in the goal object, or each object might have to query the goal each frame to see if a goal is made, or...

Comment
Add comment · Show 2 · 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
avatar image Rphysx · Apr 11, 2014 at 06:07 PM 0
Share

Specifically about "or each object might have to query the goal each frame to see if a goal is made" how do events avoid this ? Can't I subscribe the script with the function to each object ? why events would be more helpfull ? I'm sorry If I cannot explain better why things are not very clear to me

avatar image robertbu · Apr 11, 2014 at 06:18 PM 0
Share

"how do events avoid this"

Each object that listens for an event has a method that is only called when the event is fired (by the goal in my example above).

Read this answer:

http://answers.unity3d.com/questions/600416/how-do-delegates-and-events-work.html

Watch the video at the 'YoutubeVideo' link in that answer.

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.

Follow this Question

Answers Answers and Comments

21 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

Related Questions

Events/delegates not working on Android (working in Unity player though) 2 Answers

EventSystem lag on first touch 0 Answers

Setting up EventTrigger programmatically for GameObject throws NullReferenceException 0 Answers

How do I detect when UI events are triggered on *any object*? 1 Answer

Looking for some tips on performance testing 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