- Home /
Only executing the last coroutine
I have a function which has a yield in it. it's called when user clicks a button and increases a variable. however I want the instructions after my yield to be executed only once and with every click I want the previous coroutine to be terminated and only the last coroutine remain so if the user keeps clicking my button it only executes the last coroutine. here's a sample code :
var myNum = 0;
function sample(){
yield WaitForSeconds(2.0f);
myNum++;
}
Answer by smtabatabaie · Aug 31, 2013 at 07:29 AM
I did this and this way it only executes the last coroutine :
var myNum = 0;
var yieldCnt1 = 0;
var yieldCnt2 = 0;
function sample(){
yieldCnt1++;
yield WaitForSeconds(2.0f);
yieldCnt2++;
if(yieldCnt1 != yieldCnt2)
return;
myNum++;
}
Btw it's more common to do something like this:
var myNum = 0;
var yieldCnt = 0;
function sample(){
yieldCnt++;
yield WaitForSeconds(2.0f);
yieldCnt--;
if(yieldCnt != 0)
return;
myNum++;
}
Your answer
Follow this Question
Related Questions
Increment variable over time 4 Answers
Blinking & resource consumption 1 Answer
StartCoroutine gets the arglist type wrong 1 Answer
Invoke isn't working for a function with yield? 2 Answers
Yielding in a loop only as needed? 0 Answers