- Home /
The question is answered, right answer was accepted
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 :)
Answer by Hellium · Jan 25, 2017 at 05:25 PM
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
Exactly what I was about to say. So, to answer the question "for what purpose and why WaitForSeconds() accepts negative number", there is no purpose. You can't wait for something that's already happened.
thank you very much. but you sure there would be no concern about negative numbers for both WaitForSeconds/WaitForSecondsRealtime parameter??? @tanoshimi
There's no concern, because you'd never need to use them.
Answer by Eric5h5 · Jan 25, 2017 at 07:03 PM
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.
Answer by AurimasBlazulionis · Jan 25, 2017 at 06:21 PM
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).
I think you're confusing WaitForSecondsRealtime with Time.realtimeSinceStartup. WaitForSecondsRealtime is simply the unscaled time (i.e. not affected by Time.timeScale) and it's not checked more than once per frame. There is no "outside the Update" loop.
That is what I meant by the execution will not be done outside Update loop. $$anonymous$$aybe I was a bit wrong about the part when it checks the time.
Answer by UnityCoach · Jan 25, 2017 at 10:49 PM
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 :)
Follow this Question
Related Questions
Enabling multiple Monobehaviour Components in a game object 1 Answer
How can I make the health points constantly go down? 1 Answer
MonoBehaviours with same name (different namespaces) in same script 1 Answer
Problem with attaching scripts to objects 1 Answer
Unity 2D: How can i delay my clone for few seconds? 1 Answer