Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
This question was closed Nov 02, 2019 at 09:45 PM by afshin_a_1 for the following reason:

The question is answered, right answer was accepted

avatar image
1
Question by afshin_a_1 · Jan 25, 2017 at 05:22 PM · c#scripting problemwaitforsecondsmonobehaviournegative

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 :)

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

4 Replies

  • Sort: 
avatar image
2
Best Answer

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

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image tanoshimi · Jan 25, 2017 at 06:07 PM 1
Share

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.

avatar image afshin_a_1 · Jan 25, 2017 at 06:29 PM 1
Share

thank you very much. but you sure there would be no concern about negative numbers for both WaitForSeconds/WaitForSecondsRealtime parameter??? @tanoshimi

avatar image tanoshimi afshin_a_1 · Jan 25, 2017 at 06:30 PM 0
Share

There's no concern, because you'd never need to use them.

avatar image
1

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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).

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Eric5h5 · Jan 25, 2017 at 07:05 PM 0
Share

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.

avatar image AurimasBlazulionis Eric5h5 · Jan 25, 2017 at 09:34 PM 0
Share

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.

avatar image
0

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 :)

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Follow this Question

Answers Answers and Comments

11 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Unity 2D: How can i delay my clone for few seconds? 1 Answer

How do c# scripts get accessed by the Unity Engine 2 Answers

No Monobehaviour scripts in files 1 Answer

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges