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
0
Question by boddole · Mar 11, 2014 at 11:34 PM · c#programmingevent

Relationship between Events and Update()

Hello everyone, I'm starting to work with events but a I had a question that I a not seen directly addresses (and I apologize in advance if this question is strange or irrelevant).

-In terms of performance, is having a script that is subscribed to an event better or worse than say that same script checking for a condition in Update()?

--In my head, it would seem that the subscribed script would basically be waiting for a event every frame (or how would it know when the event was fired?). Or is the subscribed script just "available" and being subscribed just gives the event a memory reference to that method (and others)?

Any clarification is appreciated.

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Foestar · Mar 11, 2014 at 11:51 PM

I found that honestly in some cases it doesn't really matter. However, I have had times where scripts in my update have slowed the game down a bit. Now sure why as the scripts really weren't that sophisticated. Never had it happen with a simple event.

That said, keep in mind "Events correspond to user input (key presses, mouse actions), or are UnityGUI layout or rendering events." It's constantly checking to see if the required trigger of the event has been triggered.

Not sure if this helped.

Comment
Add comment · Show 6 · 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 boddole · Mar 12, 2014 at 01:22 AM 0
Share

So, in simple terms: don't jam a program with Update() calls, and also don't jam a program with Events? Even if Events are less intensive - something like that?

I'm just thinking that for a system that needs lots of object's scripts subscribed to events, its really no better than a program with tons of Update() calls (since each scrip on an object is just waiting for something to happen each frame).

So, if a player was in a scene of objects, and 1 of the objects killed the player - it would be better to have that 1 object poll for player health then call reset methods in all the cubes (say those are in an array or something so you have reference to them) than it would be to have that 1 object fire an event since all the other objects are just waiting for something to happen?

avatar image Foestar · Mar 12, 2014 at 01:40 AM 0
Share

I would think it would be worse to jam up the program with update calls than it would be for events. The program is always looking for events to be triggered. But the effect of those events don't apply until triggered. So while it's always looking it isn't doing much else.

With the update function you are always doing whatever is in the update. That means rather than just checking for activated events it's doing full statements and such. Almost like the effects are already triggered. And when there are hundreds of triggers going off at once it can boggle it up a bit.

So ins$$anonymous$$d you call them in events so they only trigger when necessary.

Well at least that's how I see it.

avatar image boddole · Mar 12, 2014 at 02:10 AM 0
Share

Interesting, so events still have less impact than say something like: (sorry about the formatting, I'm losing the fight to Unity's formatting structure)...

 void Update()
 {
 if (condition is true)
 {
 call specific method
 }
 }
avatar image Foestar · Mar 12, 2014 at 02:39 AM 0
Share

Yes because it's constantly running this statement at all times. Now because there's not much for this statement it's not gonna be a noticeable difference in most cases. Since the statement is stopped at the condition if it isn't met.

Essentially think of events as a statement stored to the side and waiting to be used. Unless it's something you need right away wouldn't it be easier and more efficient to stash it to the side until needed rather than carry it and make it a burden? This is how I view the difference between update and events.

avatar image boddole · Mar 12, 2014 at 03:45 AM 0
Share

"Yes because it's constantly running this statement at all times" -That is more or less what I really was getting at, I had thought that events were like updates in that they sat around going "Am I needed...am I needed...etc", but really its more like any public method in a script, it just is around until something references it. So ins$$anonymous$$d of the Update flow (code -> sees if it is needed) it would be more like (delegate that knows the code is needed -> subscribed code), where the subscribed event is allocated to memory?

And by the way, thank you for ongoing dialogue, really appreciate it.

Show more comments
avatar image
0

Answer by SirCrazyNugget · Mar 12, 2014 at 02:47 AM

Use events whenever possible. As the events are (or at least more likely to be) written in a lower level language they'll require less processes.

The difference will be mostly unnoticeable but avoidable processes should be avoided (without sacrificing some readability).

Comment
Add comment · Show 1 · 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 boddole · Mar 12, 2014 at 03:48 AM 0
Share

I see, if possible, could you take a look at my post above that quotes Foestar and see if I'm thinking in the right direction?

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

Multiple Cars not working 1 Answer

How can an editor script know when another script was removed from the project? 1 Answer

Adding single Event listener to multiple ui buttons via scripting 1 Answer

IOS touch vs OnMouseDown & error messages 0 Answers

Why is this check executing twice? 2 Answers


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