Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by cookies · Nov 03, 2011 at 01:11 AM · waitforsecondsdelay

WaitForSeconds, only pausing once

I have a WaitForSeconds (which I use to rotate something for a fix amount of time, then reverse the rotation) but it happens once, then it keeps calling rotateSpeed *= -1; and skips the yield return new WaitForSeconds(waitFor);

 void Update()
 {
     RotateCamera();
 }

 private void RotateCamera()
 {
     transform.Rotate(Vector3.forward * Time.deltaTime * rotateSpeed);
     StartCoroutine(Timer());
 }

 private IEnumerator Timer()
 {
     yield return new WaitForSeconds(waitFor);
     rotateSpeed *= -1;
 }
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

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by gfr · Nov 03, 2011 at 01:30 AM

As written you start a new coroutine every frame, leading to rotateSpeed *= -1; being executed roughly every frame after waitFor has passed.

If you want the coroutine to run only once, set a flag to remember that it is already running.

Comment
Add comment · Show 5 · 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 cookies · Nov 03, 2011 at 01:34 AM 0
Share

@gfr thanks! I don't want it to run only once, but I want it to be paused for a set amount of time (int waitFor). I want it to rotate one way for 3 seconds, and then to hit the rotateSpeed *= -1 then to rotate the other way for 3 seconds... and continue this forever!

avatar image cookies · Nov 03, 2011 at 01:37 AM 0
Share

oh i see what you are saying though. I am creating so many Timer coroutines, that once the 3 seconds is done, the other coroutines 3 seconds are finished as well.

avatar image gfr · Nov 03, 2011 at 01:39 AM 0
Share

You only need one - that's where loops come in, e.g. something like:

 while (true) {
     // yield ...
     // do stuff
     // some exit condition ...?
 }
avatar image cookies · Nov 03, 2011 at 01:45 AM 0
Share

excellent! thanks! should've figured that the routine was getting called a million times, thought it was called only after the waitforseconds was done.

avatar image paste · Nov 29, 2011 at 09:39 PM 0
Share

An alternative would be to call it in the Awake() function. That way you won't have the overhead of another variable.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

6 People are following this question.

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

Related Questions

JS WaitForSeconds Not Working? 1 Answer

Make delay for spawn 3 Answers

Waiting for fractions of a Second 1 Answer

Coroutine confusion 1 Answer

How do i add a delay to how often a unity ui button may be used? 1 Answer


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