- Home /
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?
Answer by Bunny83 · Mar 11, 2018 at 08:42 AM
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);
}
}
Thanks a lot! Also adding for anyone else who might have a similar situation, to add the additional time (So if I was at 4 seconds and activated again, I would want 14 seconds), I simply added a line right before this code.
float invincibleTimeLeft;
IEnumerator InvincibleActivate() {
if (PC.GetInvincible ()) {
invincibleTimeLeft += 10;
}
if (!PC.GetInvincible()) { // If not invincible already, become invincible.
PC.SetInvincible (true);
for (invincibleTimeLeft = 10; invincibleTimeLeft > 0; invincibleTimeLeft -= Time.deltaTime) {
print ("I'm invincible! " + invincibleTimeLeft);
yield return null;
}
PC.SetInvincible (false);
}
}
What if I have more than 1 type of power ups? xd
@UnityCosta Well in that case you can simply run different CoRoutines. So this coroutine is for invincibility, but you could have another coroutine that does more or less the same thing, but with some other powerup. $$anonymous$$ake sure you @Larcondos If you want me to see your reply!
Answer by hexagonius · Mar 11, 2018 at 10:12 AM
You could also check CustomYieldInstructions. you could rewrite a waitforseconds based on that but with an additional time remaining getter.
Right, but be aware that this would add additional overhead. CustomYieldInstructions do start a new nested coroutine. To get access to the time variable you have to store it somewhere outside the coroutine. So just using a simply loop inside the coroutine is the most efficient. Also be careful when you try to "reuse" custom yield instructions. You can not use the same instance twice at the same time. Also you would have to take care of "resetting" the instance before reuse. It's usually meant to be recreated for each use. But as i said even when you reuse the custom yield instruction instance you will create a new coroutine each time you "yield" your instance.
Your answer
Follow this Question
Related Questions
Making a timer using WaitForSecondsRealtime without keyword 'new'? 1 Answer
C# simple delay execution without coroutine? 2 Answers
SpeedBoost won't reset : Problem with either WaitForSeconds or Coroutine (Solved) 2 Answers
How to make WaitForSeconds work as a repeating/looping timer? 1 Answer
Coroutines WaitForSeconds() won't work,WaitForSeconds() isn't doing anything 0 Answers