How to get Coroutine's remaining time in WaitForSeconds?

Hey everyone!

So my issue is that for my invincibility powerup that I am currently using, I have a coroutine which works as follows:

IEnumerator InvincibleActivate() {
		if (!PC.GetInvincible()) { // If not invincible already, become invincible.
			PC.SetInvincible (true);
			yield return new WaitForSeconds (10);
			PC.SetInvincible (false);
		}
	}

The only problem with this, is that it’s possible for two “invincibility” powerups to spawn at the same time. As such, a user could pick up two powerups, but would still only be invincible for 10 seconds.

Is there any way that I can get the remaining time left in the “waitforseconds” and add it to a new counter? Or am I going about this particular problem completely wrong?

You can’t this way since WaitForSeconds is handled internally by the scheduler. If you want a progress you want to do something like:

float invincibleTimeLeft;

IEnumerator InvincibleActivate() {
    if (!PC.GetInvincible()) { // If not invincible already, become invincible.
        PC.SetInvincible (true);
        for(invincibleTimeLeft = 10; invincibleTimeLeft > 0; invincibleTimeLeft -= Time.deltaTime)
            yield return null;
        PC.SetInvincible (false);
    }
 }

You could also check CustomYieldInstructions. you could rewrite a waitforseconds based on that but with an additional time remaining getter.