- Home /
Having lots of Update Functions
is it bad practice (too costly) to use lots of update functions across the game world. or does it not matter a lot
thanks
Answer by asafsitner · Dec 16, 2012 at 08:58 PM
Obviously Unity is designed around the idea of Update (and the other event methods like OnGUI or Start), but it's also obvious that having many events can hurt performance. My advice is: If you don't need something, don't have it there. If the component doesn't have code inside it's Update method, remove it.
Other than that, if you have an extremely high number of things that happen in our game, you should ask yourself if they all need to happen every frame, and if not, implement your own "job scheduler" (by the way using coroutines to do that for you can cause even greater performance impact than just using Update, depending on the number of coroutines you have and what they do exactly) or some other manager of that sort.
I've been using quite a lot of Update calls myself (>2000) and the call itself was not the bottleneck.
The call itself is a question of few cycles so it is nothing. As mentioned by @asafsitner, it is what is inside that matters.
The overhead for individual Update calls does actually add up to a noticeable amount with a lot of objects.
Answer by Eric5h5 · Dec 16, 2012 at 09:22 PM
A call to Update has overhead, so if you have 1000 objects with Update, then you have that overhead X 1000. If you have a lot of objects that do need updating every frame, it would be more efficient to have a single Update function that uses a loop to iterate though the objects and performs the necessary operations for each one inside the loop.
Agreed 100%.
But, as I said, you'd have to identify the bottleneck in your game. In my case it wasn't the overhead, but rather the code (and Camera.Render which took ~20ms in itself...)
As a side note, in earlier versions of Unity the overhead to calling OnGUI (even an empty method) was tremendous, even if it was only called once. I haven't used GUI as heavily since, but from what I've heard it should have been fixed (i.e. it's still terrible but much better than before).
Your answer
Follow this Question
Related Questions
Efficiency of Game Loops 2 Answers
How to code input. 1 Answer
How would i change this string in update to a StringBuilder? 2 Answers
Do something when health is below X 2 Answers
Setting Text.text in UI efficiency? 1 Answer