- Home /
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.
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...
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.
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.
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.
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;
}
}
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;
}
Clever using the second "running" bool - took me a bit to break it down since I'm still a coroutine rookie . ;) Thanks!
Your answer
Follow this Question
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