Should I use a Coroutine function or a Time.deltaTime equation to add to values overtime?
I'm trying to make an incremental game of sorts. As you may know, incrementing numbers is a huge part of this task and should be done as efficiently as possible. I've come across two seemingly similar ways to do this that both seem viable and popular. The first way is simply using a Coroutine and having it add a certain value after a yield return new WaitForSeconds(waitTime)
command. The second way is to just constantly add values using something like value += amount * Time.deltaTime
and putting it in the Update function. I'd like to know which would be less resource intensive since I'll be adding tens to hundreds of these, or possibly a new way that is more efficient than both of my known methods. Any information on this topic would be great, as I have not found much in my search queries. Edit: I thought I would also mention that I am programming in C# and would prefer any code given in C#, although I could transpose if need be.
Edit 2: I also realize you could call an InvokeRepeating()
call and simply pass in a function that adds a certain value at a set interval. I am still unsure of how any of these three methods compare in efficiency. Currently, it would seem that the Time.deltaTime equation would be the least resource intensive since it is just a simple equation rather than a whole function, but I am not too sure about that reasoning. At this moment in time, I will likely implement my incrementing functions using that equation and see how it goes. I will be using the general equation of:
value += (valueToAddEachInterval * (1 / timeInterval)) * Time.deltaTime;
This will allow me to increment a value by a certain amount in a given interval, such as 5 every 12 seconds. I am still eager to hear back from other users and the techniques they use for such implementations.
Answer by jdean300 · Sep 17, 2016 at 12:15 AM
Doing this inside of update will likely have better performance than using coroutines - however, the difference will be extremely small. Even doing this hundreds of times concurrently is trivial. You won't notice the difference until you're doing it on the order of a few thousand times simultaneously, if at all.
I am pretty confident that this will not be the performance bottleneck of your game. Write whatever code is easiest for you to understand and when you start having performance problems profile your code to determine where you should optimize. This smells like premature optimization which is commonly referred to as "the root of all evil".
Thank you for your answer. I had heard a few rumors that multiple coroutines could drastically decrease performance, although, I wasn't sure how reliable that information was. I was mostly just curious if there was a different more optimal or easier way to implement this functionality. If you say it is $$anonymous$$uscule in performance then it seems I have nothing to worry about.
I haven't had an issue running many coroutines at once - what may be an issue (I have no idea, just speculating) is if you have many coroutines executing on one object.
Your answer
![](https://koobas.hobune.stream/wayback/20220612092936im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Time.deltaTime and difference of Time.time not matching 0 Answers
Improving Click-to-Move Movement Script (2D) 0 Answers
Unity coroutine movement over time is not consistent/accurate? 1 Answer
Getting variable from script while keeping other code from it 1 Answer
How to deactivate a dropdown list when changing UI tabs 0 Answers