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 Panamamigo · Jul 27, 2010 at 04:58 AM · coroutineupdatepausewaitforseconds

Coroutines, WaitForSeconds with function Update?

I am having trouble with a coroutine. The bottom script is not my actual script, but a watered down version because the actual script is very long and boring. Basically I have an if/else statement and the else part I want my update to do something, do the function with the coroutine, and then continue back down the line. The problem I face is that Once it gets to the yield WaitForSeconds part is skips the rest of the coroutine and goes back to the Update. Is this because I am making the function mycoroutine yield back to update or what? How can I make a pause in my line of commands. Thank you for the help. I've been trying really hard and looking for already posted coroutine questions but they haven't been much help.

function Update() { if(whatever) { Do something; } else { Debug.Log("Reached else"); MyCoroutine(); Debug.Log("Done with else"); }

}

function MyCoroutine() { Debug.Log("Coroutine started"); yield WaitForSeconds (5); Debug.Log("Coroutine finished"); }

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

3 Replies

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

Answer by Eric5h5 · Jul 27, 2010 at 06:20 AM

You can't use coroutines like that from Update. You can start coroutines in Update, but you can't yield on them (since using yield can only be done in a coroutine). Update happens once every frame regardless, and will never pause for anything. When scheduling a sequence of events with coroutines, it's much simpler just to avoid using Update entirely, since it's not appropriate for that kind of code.

Comment
Add comment · Show 7 · 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 Panamamigo · Jul 27, 2010 at 04:36 PM 0
Share

Thank you for your response. It is doing the same thing even when using StartCoroutine or not using it at all. It goes into the my coroutine function and executes everything up until the yield to make it wait for two seconds and then it gets skipped over and goes back to the function Update. What should I do to get the same effect of making it wait for two seconds? What can I do?

avatar image Eric5h5 · Jul 27, 2010 at 09:40 PM 0
Share

@Panamamigo, StartCoroutine is unnecessary in JS. As I said, you just can't use Update for this sort of thing, at all. Remove the Update function, and stick with coroutines.

avatar image Panamamigo · Jul 28, 2010 at 12:06 AM 0
Share

So basically I would make a coroutine called $$anonymous$$yUpdate() or something and that would run every frame unless I told it not to or to yield?

avatar image Eric5h5 · Jul 28, 2010 at 12:37 AM 0
Share

@Panamamigo, it depends on what your code is doing, but that's a possibility. For example, this code checks every frame for input, and once it gets some, then it performs another routine for a while, then goes back to checking input: http://www.unifycommunity.com/wiki/index.php?title=Grid$$anonymous$$ove

avatar image Panamamigo · Jul 28, 2010 at 02:57 AM 0
Share

Well what I'd like to do for the code in the original question I had asked is to change the function Update() to a coroutine so it wouldn't matter if I made it yield or anything. I just don't know how to make a coroutine that acts just like function Update and haven't been able to find anything on the web either.

Show more comments
avatar image
1

Answer by Statement · Mar 12, 2011 at 04:44 PM

I just wanted to post a link to CoUpdate, an alternative approach of using Update that support yield instructions. I think this is a common enough problem that has been asked several times so I'll add this link to related questions.

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
-4

Answer by qJake · Jul 27, 2010 at 05:17 AM

You aren't starting the coroutine properly. You need to use StartCoroutine("MyCoroutine");

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 · Jul 27, 2010 at 06:18 AM 1
Share

Not in Javascript you don't.

avatar image SARWAN · Apr 11, 2013 at 01:31 PM 0
Share

Apart from Yield Waitforseconds. Is that any other ways to pause the game????

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

1 Person is following this question.

avatar image

Related Questions

How to make a spawner wait for x seconds? 2 Answers

How to create a delay in a function being called in update? 2 Answers

Is there a way to make WaitForSeconds ignore time.timescale? 1 Answer

Yield WaitForSeconds Not Working. Coroutine. 1 Answer

Load Level when dead for 1 second 2 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