Update vs Coroutine vs EventHandler

I’m creating a game with lots of GameObjects that independently require timers when interacted with. I’m wondering which of the following is the most efficient use of a computer’s processing power:

  1. An update function attached to each that keeps track of its timer
  2. A coroutine that starts when interacted with (that keeps track of the timer)
  3. An EventHandler attached to a single timekeeping script to send an event to all subscribed delegates every second. Each gameObject would be subscribed and increase an int timer value on each event

I’m wondering at what scale each is the best option. I understand the third gives me less flexibility with the timer because it will only conclude on intervals of seconds whereas the first two can conclude on intervals between single frames but I have no idea if an Event System demands much from the computer or if coroutines demand more than update functions when running. Any help is much appreciated!

Just use whatever makes it easiest to read. You only really need to worry about performance once it starts becoming a problem.

Do not even think about this until you have made at least a dozen games. Seriously.

You need to focus 100% on understanding exactly what is going on. Optimization will make that almost impossible.

DO NOT OPTIMIZE “JUST BECAUSE…” If you don’t have a problem, DO NOT OPTIMIZE!

If you DO have a problem, there is only ONE way to find out. Always start by using the profiler:

Window → Analysis → Profiler

Failure to use the profiler first means you’re just guessing, making a mess of your code for no good reason.

Not only that but performance on platform A will likely be completely different than platform B. Test on the platform(s) that you care about, and test to the extent that it is worth your effort, and no more.

https://discussions.unity.com/t/841163/2

Remember that optimized code is ALWAYS harder to work with and more brittle, making subsequent feature development difficult or impossible, or incurring massive technical debt on future development.

Notes on optimizing UnityEngine.UI setups:

https://discussions.unity.com/t/846847/2

At a minimum you want to clearly understand what performance issues you are having:

  • running too slowly?
  • loading too slowly?
  • using too much runtime memory?
  • final bundle too large?
  • too much network traffic?
  • something else?

If you are unable to engage the profiler, then your next solution is gross guessing changes, such as “reimport all textures as 32x32 tiny textures” or “replace some complex 3D objects with cubes/capsules” to try and figure out what is bogging you down.

Each experiment you do may give you intel about what is causing the performance issue that you identified. More importantly let you eliminate candidates for optimization. For instance if you swap out your biggest textures with 32x32 stamps and you STILL have a problem, you may be able to eliminate textures as an issue and move onto something else.

This sort of speculative optimization assumes you’re properly using source control so it takes one click to revert to the way your project was before if there is no improvement, while carefully making notes about what you have tried and more importantly what results it has had.

How many is “lots”? Until a certain point, none of the above will likely make a difference. Which is why you will get the correct recommendation: prioritize readability and maintainability.
After a certain point however, no matter which of these you use it will break down due to the amount of gameobjects.

1 Like

Good to know! I’m new at development and know very little about what I should be prioritizing so this is super helpful.

Awesome… you should throw yourself at all kinds of random different little games. Try it all… try little test scenes that prove out a single thing, like animate a cube bobbing back and forth, then give the player control of that cube with WASD… the more random little mini- or micro-projects you tackle, the quicker you will develop a sense of what you’re working with, and the quicker you’ll find solutions for the games you really want to do.