WaitForSeconds() and WaitForSecondsRealtime() negative number concern!!!!

Hi. first of all i’m beginner to coding and also beginner to unity.

i’m using c# and i have a question about WaitForSeconds(float seconds) and WaitForSecondsRealtime(float seconds) . as you know they suspend the coroutine execution for the given amount of seconds. my question is about this float parameter that seems it only have to be a positive number like:
yield return new WaitForSeconds(3.0f); //it waits for 3 second
yield return new WaitForSeconds(0.5f); //it waits for 0 point 5 seconds
same as for WaitForSecondsRealtime.

i’v never thought about negative numbers till now. because i thought i’ll get an error with the YOU HAVE TO USE ONLY POSITIVE NUMBERS description. but i’m checking them now and they magically accept negative number. in my script i play with a variable, then i give it to WaitForSeconds float parameter. sometimes somehow my variable changes to negative(not important how, don’t talk about it please). what happens to yield return new WaitForSeconds(parameter) if its parameter is a negative number? for example:
yield return new WaitForSeconds(-20.0f);
or
yield return new WaitForSeconds(-1.5f); //same as for WaitForSecondsRealtime()
i used the both lines above and nothing happened.(it’s like unity MonoBehaviour or C# or something else is changing it to 0 ). tell me is that true?? if so then i shouldn’t be worry but for what purpose and why WaitForSeconds() and WaitForSecondsRealtime() accept negative number?

I welcome and appreciate any help :slight_smile:

There is no point using negative numbers in WaitForSeconds (so don’t do it), but nothing bad can happen. Unity simply looks at the elapsed time and compares it to the time you specified. Since any amount of time will be greater than a negative number, the function stops immediately.

I guess Unity saves the current timestamp (t0) when yield return new WaitForSeconds(-1.5f) is called and checks every frame if Time.time - t0 > -1.5, which is always true

It might wait for the next fixed update, but that is it. Unity does checks inside FixedUpdate or Update loop if the current time minus start time is greater than the wait time, which is always true in your case. The only difference you might get is with WaitForSecondsRealtime, because it should do the checks outside the Update or FixedUpdate loops, but the actual code will still be execution should not differ that much (it does not execute between updates).

AFAIK, there is not filter to methods parameters. So they couldn’t declare it like WaitForSeconds (positiveFloat time).

But yes, like the others said, they surely catch negative values :slight_smile: