- Home /
Performance when having Update functions in many gameObjects ?
Is it bad for performance to have Update functions in many gameObjects or should I make one script with Update that loops trough those gameObjects and pass variables to them if needed? And if there is a performance problem difference, how significant it is? If I have circa 50 gameObjects, does it any make difference between those methods.
I have done some coding with AS3 earlier and it was bad to have lot of enterFrame funcitions in different movieClips ( flash equivalent to gameObject and Update).
Answer by save · May 19, 2012 at 01:24 PM
No that's no biggie, just make sure to disable objects and components that isn't needed. Some parts of the logic maybe could live inside an invoked repeated function if it's distance checking and such tasks.
Just to give you an idea we have about 50 rigidbodies within a scene with Update- and FixedUpdate-function, animation scripts running alongside and distance checking invoked running on iPhone 3GS devices. Unity is greatly optimized from the beginning, you just have to think about the target limitations.
+1. The overhead of having many Update is infinitesimal if the amount of work is the same but emptying your Update in favor of custom InvokeRepeating() method may make the app considerably faster.
hey save one doubt, if i run 200 lines of code in one update and also run the same amount of code in 5 or 6 files, is there a difference in performance? atleast a bit?
Yes it will be a performance hit as they all run in the same thread. But it really depends how you've structured it, the amount of code is insignificant to how you actually use it. It's not recommended to run everything within the Update-loop, a couple of if's which executes functions when needed. Lifting code out to functions is really important so you can yield or invoke.