- Home /
check bool in WaitForSeconds
Are there a method for check ture/false in the WaitForSeconds function? When the boolean is changed and do something immediately
Answer by xxmariofer · Jul 31, 2019 at 10:14 AM
IEnumerator Example()
{
yield return new WaitUntil(yourBool);//this will wait UNTIL your bool is true
Debug.Log("Your bool is true add here your code");
//if you need to make sure it only gets excuted during x amount of time add a timer
int timer = 0;
while(timer < 5)//will wait for 5 seconds
{
if(yourBool)//if your bool is true break the while
{
Debug.Log("Your bool is true add here your code");
break;
}
timer += Time.deltaTime;
yield return null;
}
}
Answer by hameed-ullah-jan · Jul 31, 2019 at 10:04 AM
You must be changing boolean some where in your code, so at that place you can call any function and do something in there. I'm not getting your point why are you asking about waitforseconds, if you can elaborate your question may be i can help after that.
I want to do something like that in IEnumerator public void example(){
bool gonext = false;
float timer;
timer += Time.deltaTime;
//some trigger to turn gonext true
if(timer >=5 || gonext == true){
//some action
break;
}
}
IEnumerator Example() { int timer = 0;
while(timer < 5)//will wait for 5 seconds
{
if(yourBool)//if your bool is true break the while
{
Debug.Log("Your bool is true add here your code");
break;
}
timer += Time.deltaTime;
yield return null;
}
}
the second part of the code i posted is what you are looking then
When the yourBool = false as default in the programme. It can not load under the code. When the yourBool = true, it go to the Debug.Log("Your bool is true add here your code"); and break immediately.
Your answer
Follow this Question
Related Questions
Better way to delay a function for a few seconds? Javascript 1 Answer
How to create a delay in a function being called in update? 2 Answers
having a dilemma with a canShoot bool in an fps 3 Answers
Call a function repeatedly while a boolean is true 3 Answers
boolean as function parameter/argument 2 Answers