- Home /
Proper Way to Wait in C#
I'm writing a game in C# and there's a few times where I have to wait a second or two for something. So I've been using yield return new WaitForSeconds(1.5f);
However, if I put this in my OnTriggerEnter function, I need to have it defined as IEnumerator
instead of void
. This isn't always acceptable, so I'm wondering if there's some other way to wait for a specified amount of time without having to change the return type on the function I'm writing it in.
EDIT: I've tried creating an extra function that does the waiting, but it just seems to not actually wait for the time I've specified. Here's the function:
IEnumerator wait(float time)
{
yield return new WaitForSeconds(time);
}
And here's how I've called it:
changeColor(Color.red);
loseHealth(1); //lose health
canTakeDamage=false;
StartCoroutine(wait (1.5f));
Debug.Log ("done waiting");
changeColor(Color.white);
canTakeDamage=true;
The code is delaying when a player can be hit consecutively by an enemy. It also changes the colour to red when it can't be hit, and back to white when it can be hit. When I try it this way, the colour never actually changes, and the player can take damage again immediately (rather than 1.5 seconds later).
Answer by fafase · Mar 25, 2013 at 04:48 PM
"I need to have it defined as IEnumerator instead of void. This isn't always acceptable"
Would this fix it?
void CallingFunction(){
StartCoroutine(WaitingFunction());
}
IEnumerator WaitingFunction(){
yield return new WaitForSeconds(1.5f);
}
void OnTriggerEnter(){
CallingFunction();
}
This is not really cute but you get the void function you seem to need.
Thanks. Unfortunately though, I tried this (edited my question). I don't necessarily NEED the void function in this case, but say I'd like to call the waiting function from another function (just for neat code purposes). For example, I have a function called loseHealth(int health)
and I want the character to be flash red then back to white. I have to put a small wait in there to accomplish the flashing. Why would I want this wait code to be written outside of the loseHealth
function if I'm going to use it every time he loses health? That's just one instance, I was just wondering if there was a neater way to do it. Ideally, your method (and $$anonymous$$hada suggested it also) should work, but it doesn't seem to. $$anonymous$$aybe I'm doing something wrong?
so you want the health to flash red and then back to white right
yes that is exactly what I want. Or even flashing between red and white for a few seconds.
hi so is the red texture applied to whole screen or just the player health bar and you want it to be red when the player is hit or not hit explain a bit more about the above
When the player is hit, I want him to turn red for about a second and a half, then turn back to white. The waiting doesn't work if I use StartCoroutine, so I have to do the yield wait right in the OnTriggerEnter function. I'd rather do this in a different way.
Your answer
![](https://koobas.hobune.stream/wayback/20220613095048im_/https://answers.unity.com/themes/thub/images/avi.jpg)