Bool function with timer won't return value after timer but instantly.

Hello,

I am trying to create a fucntion that returns true a bool after a given ammount of time. I am sorry if it was already asked but i could not find anything.

bool SetInputToTrue(float sec)
{
     float _timer = 0;

     while(_timer < sec)
     {
         _timer += Time.deltaTime;
     }
     return true;
}

When i call it “canInput = SetInputToTrue(0.5f);” it simply return the value instantly.

Thanks.

You need a coroutine function to do that and it doesn’t allow a return value.

You would need a callback if you want something set to true:
https://www.reddit.com/r/Unity3D/comments/3vavkp/using_callbacks_in_coroutines/

This part does it instantly. I’m not sure what you’re trying to achieve. Do you not know how while statements work? Did you mean to place an “if” instead of a “while” there?

I did try with an if too and it’s the same thing. I have to stick with Coroutines unfortunately.

If you’re still unsure about why your code didn’t work, it’s because the program runs the while loop until the condition is false. There is no delay (other than execution time). The same would be true for any loop; you wouldn’t imagine that a loop from 1 to 100 would somehow delay itself.

No, you can do it without co-routines.
It’s just not as elegant.