Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Zanolon · May 10, 2018 at 02:10 PM · waitforsecondsienumerator

Pause in C#

I think I'm doing this right.

 IEnumerator Wait()
 {
     yield return new WaitForSeconds(5);
 }

 void OnTriggerEnter2D(Collider2D col)
 {
     if (col.gameObject.name=="StopBlock")
     {
         Storage = FindObjectOfType<PlayerMovement>().speed;
         FindObjectOfType<PlayerMovement>().speed = 0;
         StartCoroutine("Wait");
         FindObjectOfType<PlayerMovement>().speed = Storage;
     }
 }

This should work but it doesn't. There is no error, it's just not stopping. I don't know why.

Comment
Add comment · Show 1
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 Derptastic · May 10, 2018 at 09:34 PM 0
Share

Don't forget to mark an answer if it worked for you, so people who end up here know what helped you.

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Kciwsolb · May 10, 2018 at 02:17 PM

Hi, when you start coroutine Wait(), it will still proceed to the next line of code immediately because you do not have it doing anything before it yields control back. If you want to actually wait for some amount of time and then do your Find, you need to put that line of code IN the coroutine after your wait.

 IEnumerator Wait(float Storage) //Pass in your parameter 
 { 
           yield return new WaitForSeconds(5); 
           FindObjectOfType<PlayerMovement>().speed = Storage;
 }

Then in OnTriggerEnter2D call it this way:

 StartCoroutine(Wait(Storage));

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 PizzaPie · May 10, 2018 at 02:39 PM 2
Share

Coroutines does not run on a seperate thread, they run on the $$anonymous$$ain thread. Now when you start a coroutine the code will run normaly till a yield instruction is reached control will return to the caller. The engine keeps track of the coroutine and when the "condition" of the yield has been fulfilled the control will return on the point where it had left inside the coroutine, because it is run on the main thread the order (ti$$anonymous$$g) of the return will be according to this. It is more of pseudo threading, but quite far from it.

avatar image Kciwsolb PizzaPie · May 10, 2018 at 02:44 PM 1
Share

Bad wording on my part... I removed the thread wording. Should be more accurate now. Thanks!

avatar image
1

Answer by Derptastic · May 10, 2018 at 02:23 PM

(Some formatting of that code for readability would be appreciated.)

Are you trying to pause the whole game? Then it's

Time.timeScale

that you want.

Your code is not quite right either. What you're doing is starting a co-routine that's waiting for itself. When you call it, the co-routine starts, waits 5 seconds before continuing itself while the rest of your code continues immediately after the call, setting the player speed back to it's original setting right after you set it to 0. That's the point of co-routines, to perform async tasks.

If you insist on keeping that logic, just put the rest of the code in the "Wait" routine as well. Your code:

 IEnumerator Wait()
     {
         Storage = FindObjectOfType<PlayerMovement>().speed;
         FindObjectOfType<PlayerMovement>().speed = 0;
         yield return new WaitForSeconds(5);
         FindObjectOfType<PlayerMovement>().speed = Storage;
     }
     void OnTriggerEnter2D(Collider2D col)
     {
         if (col.gameObject.name == "StopBlock")
         {
             StartCoroutine("Wait");
         }
     }

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

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

84 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Coroutine doesn't work when called from another method. 3 Answers

How to make a 3d text writeOut method? 1 Answer

Loop with WaitForSeconds in IEnumerator appears to be incorrect by 10-15% 2 Answers

Scipt Not Functioning Properly 1 Answer

Problem with coroutine 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