lets say you have a bunch of scripts running.
all with things happening on Update.
does that impact performance? especially on mobile devices.
if so, would it be helpful to create a separate “update” script that had a timer that triggered “updates” on other scripts that need frequent updates but not every frame, lets say every 5 seconds?
reason i ask, is i am new to unity, new to game programing in general, but do have allot of programming experience. working on game that is primarily a pc game, but thought about making it mobile as well.
and was curious if i should plan for something like this, or if the constant updates from countless scripts impact performance at all
and if the 5 second update would help, how hard would it be to create a unity plugin to help with this?
lets say you right click on an object with a script that you want on the 5 second update. and it changes the void Update to something like void delayedUpdate and then it added a reference to that script in your timedupdate script.
If you don’t need to execute code every frame in a object (update) you should use coroutines. Coroutines do exactly what you are proposing.
For example, if you wanted an object to do something every five seconds you would do:
void Start()
{
StartCoroutine(FiveSecondUpdate());
}
IEnumerator FiveSecondUpdate()
{
WaitForSeconds f = new WaitForSeconds(5);
while(true)
{
// do stuff
// wait for another five seconds
yield return f;
}
}
thanks, good to know that’s their, its something you don’t really come across unless you know exactly what you are looking for. just like I had no idea how to make sure specific script executed before EVERYTHING else. just found it was possible to drag something above the “default time”…
until now i was adding everything that referenced my database into the execution order. got a little frustrated on the 20th script, now the only thing in their IS the database, above the default time.
C# events are like magic for game initialisation! I’ve always found messing around with execution times is completely overkill and too far hidden from everyday work. Static events for systems are my solution. Just make sure to null the static events out when changing scenes otherwise you will be running out of memory very quickly, haha
Update is found via reflection but I believe references to those methods are cached so it’s not doing reflection every single frame.
That being said - IvokeRepeating and Coroutines are still polled every frame to determine their state and whether or not anything should be executed on them.
Ultimately, none of this can really be answered off the cuff. Write the code so it works and then use the Profiler to determine what is running poorly and optimize that particular piece.
@KelsoMRK I was going by benchmark data from 4 years ago, but its very likely that the Unity Team has optimized a lot of things since then. This quote was part of what gave me the idea that Update is called via reflection:
Edit: I goofed. Inspecting the examples Erich posted, they’re time comparison examples so its not really exactly a fair comparison of the performance of a Coroutine vs Update in one Cycle, executing the same code on that Cycle.
Even if you are yielding a WaitForSeconds object with a value of like 3 or 5 seconds? I suppose it depends on the speed of checking if a coroutine needs to run vs invoking Update via reflection?