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
1
Question by bkovner100 · Nov 24, 2012 at 02:32 PM · coroutineloopwhilewhile loopstopcoroutine

Stop Current Coroutine, Then Restart It

Trying to trigger a coroutine loop at random intervals, but before it starts, it needs to interrupt and exit the previous instance of it, so only one instance of the loop is running at a time:

 while (x == true)
 {
      Do_Action_Over_A_Number_Of_Frames();
      yield;
 }


Tried a bunch of different methods with no luck. Can't use StopCoroutine() since I have multiple instances of it running on other game objects. I can turn off the previous loop by making x=false but then the new loop won't start. I could have x=false followed by x=true but then the first loop will never exit and the second loop will stack.

I could disable and enable the script that contains the above coroutine but that seems like overkill.

I feel like I'm missing something really simple but scoured the Forums and Answers with no luck.

Comment
Add comment · Show 4
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 whydoidoit · Nov 24, 2012 at 02:35 PM 0
Share

There are two tutorials on Coroutines on Unity Gems that might give you some ideas.

Sounds like you should just be writing a single coroutine that randomly restarts - but perhaps I don't really understand what you are after...

avatar image bkovner100 · Nov 24, 2012 at 08:40 PM 0
Share

Thanks whydoidoit - actually looked at Unity Gems and it's got good stuff but couldn't find an answer for my particular problem.

$$anonymous$$y coroutine is called every time an object gets hit. It lerps a gameObject towards a target over time and when it get's hit, it needs to interrupt the lerp and move toward a different target.

I came up with something - check my answer and let me know whatcha think.

avatar image whydoidoit · Nov 24, 2012 at 08:42 PM 0
Share

Will do - I was thinking the "hijacked coroutines" in the advanced tutorial would allow you to switch between two - but as I say I may not exactly have been getting what you were after.

avatar image bkovner100 · Nov 24, 2012 at 09:20 PM 0
Share

Yeah - it's a great site. Was looking at that exact same thing. Unfamiliar with C# but from what I can decipher, hijacking would be great for switching between different functions. I'm trying to interrupt and re-start the same function. But again, check out the answer when it gets posted - I'd love to hear your comments.

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Wiki

Answer by bkovner100 · Nov 24, 2012 at 09:27 PM

Came up with something - it works but I'm definitely open to suggestions.

This function is called every time an object gets hit - it needs to interrupt the previous instance of the loop and start a new one.

The first line in the function turns off the previous instance of the loop, then turns back on the bool so it can run a new instance:

 function Action()
 {
      x = false; yield; x = true;
      while (x == true)
      {
           Do_Action_Over_A_Number_Of_Frames();
           yield;
      }
 }

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 whydoidoit · Nov 24, 2012 at 09:44 PM 1
Share

I'm liking that - neat approach. I might suggest this (probably not necessary for this simply cancel in a single frame case):

 var running = false;

 function Action()
 {
      x = false; 
      while(running) 
            yield; 
      x = true;
      running = true;
      while (x == true)
      {
          Do_Action_Over_A_Number_Of_Frames();
          yield;
      }
      running = false;
 }
avatar image bkovner100 · Nov 24, 2012 at 10:01 PM 0
Share

Clever using the second "running" bool - took me a bit to break it down since I'm still a coroutine rookie . ;) Thanks!

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

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

How to wait for Coroutine to finish? 2 Answers

Waiting for a mouse click in a Coroutine 1 Answer

StopCoroutine() is not stopping my coroutines 1 Answer

Breaking from a Loop 2 Answers

Problem with while loop in 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