Time.deltaTime not returning time since last frame?

while (DurationTimer < Duration){
DurationTimer += Time.deltaTime;
if (Ship.Shields < Ship.maxShields/2f){
} else {
yield return new WaitForSeconds(.25f);

First off, I have no idea why the code pasted so weird with the indents…

Duration in this case is 7f. I assumed this would then have this effect last for 7 seconds, where every quarter of a second shields are increased until 50%, then armor is increased. Looking into the documentation I’m seeing that deltaTime returns the time since the LAST frame… not the time since it was last called within scope. Sooooo… What’s a “good” replacement for deltaTime? Should I calculate my own delta time since the last loop in this coroutine, or should I have the coroutine yield WaitForFixedUpdate and keep a second timer for controlling the restore calls? Or even better – is there a built in way of getting delta time from within a coroutine?

You can approach this in a number of different ways. A timestamp would work well. Record the future time when to exit (Time.time + 7.0), and then compare against Time.time to see when the cycle is complete, but if accuracy is not paramount, there is an even simpler fix. Your issue is this line:

 yield return new WaitForSeconds(.25f);

This code means that each cycle of the while() loop will be just a bit more than 0.25 seconds. So you could just do:

 DurationTimer += 0.25f;

Or on average, you will be half a frame over. Running at 60 fps, that means you would be .0083333 over on average, so you can do:

DurationTimer += 0.258333;