Title: Coroutine acts different when called normally VS. through event handler
Tags: c#;unity3d;admob
Question: The following coroutine, when run on the press of a UI button, will run normally and show "3...2...1...0" and then disappear.  However, when run as a part of the event handler delegate HandleRewardBasedVideoClosed (part of AdMob) it will show "3...1..." and then disappear.  For the life of me I cannot figure out why it would act differently when called in these two different manners.

```public IEnumerator Countdown() {

    timeLeft = 3;

    while (timeLeft &gt;= 0) {
        countdownText.text = timeLeft.ToString();
        yield return new WaitForSecondsRealtime(1.0f);
        timeLeft--;
    }

    if (timeLeft < 0) {
        countdownText.gameObject.SetActive (false);
    }

}

public void HandleRewardBasedVideoClosed(object sender, EventArgs args){

    MonoBehaviour.print("HandleRewardBasedVideoClosed event received");

    if (reward) {
        StartCoroutine ( gm.Countdown ());
    } else
        SceneManager.LoadScene (SceneManager.GetActiveScene ().name);

}
```

Comment: Does your print statement fire twice?

Comment: Shouldn't timeLeft be a local variable in the Coroutine scope? Maybe Countdown is being called twice at the same cycle, and as the variable is not local, it's beeing decremented by both routines.

Comment: @MatrixTai I hadn't realized but yes it does seem like time is passing twice as fast and so it only count 3 and 1.  Why would this be the case?

Comment: The print statement does not fire twice.  WaitForSeconds doesn't work because Time.timeScale is set to 0 at the time this is hit

Comment: May I know if your time passed two time faster? Like just using 1.5s to count 3s when in Admob

Comment: I havent use admob before, so I want you to make another try first. Use `WaitForSeconds` instead of `WaitForSecondsRealtime`. See if the problem fixed.

Here is the accepted answer: I have a suspicion about this one. Is it that the count down is in fact doing "3.2 .. 2.1 .. 0.SetActive(false)" so quickly that you're not seeing it running the coroutine twice? If so the following code will resolve that particular problem (if that's the case):

```private bool isCountingDown = false;

public IEnumerator Countdown()
{
    if ( isCountingDown ) return;
    isCountingDown = true;

    for ( int timeLeft = 3; timeLeft &gt;= 0; timeLeft-- )
    {
        countdownText.text = timeLeft.ToString();
        yield return new WaitForSecondsRealtime(1.0f);
    }

    countdownText.gameObject.SetActive (false);
    isCountingDown = false;
}

public void HandleRewardBasedVideoClosed(object sender, EventArgs args)
{
    MonoBehaviour.print("HandleRewardBasedVideoClosed event received");
    if (reward) {
        // This might be a redundant check, as we're doing this in the 
        // Coroutine, but we may as well not go through the process of 
        // calling it in the first place.
        if (! isCountingDown ) StartCoroutine ( gm.Countdown ());
    } else
        SceneManager.LoadScene (SceneManager.GetActiveScene ().name);
}
```

Comment for this answer: I like the idea of using a for loop instead of while to try to ensure it doesn't skip.  I'll try this when I get home.  I'm still curious why it would act different when run through a button instead of the delegate
Comment for this answer: You can improve this by making `timeLeft` a local variable instead of a member variable. It seems unnecessary and dangerous to not have it this way (which is likely also one of the root causes of his problem).
Comment for this answer: You've got an event handler in place. Your ad manager might call the event twice, for "whatever" reason. In addition, when you press the button, are you calling the coroutine, and then the ad manager ALSO calls the routine? Did you clean up the code after yourself and your tests? Without further information, it's hard to tell, but these are possible reasons. An event or method doesn't get called twice for no reason.


{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 1, 'CommentCount': 6, 'ContentLicense': 'CC BY-SA 4.0', 'CreationDate': '2018:00:07/12/18', 'FavoriteCount': 2.0, 'PostTypeId': 1, 'Tags': '<c#><unity3d><admob>', 'Title': 'Coroutine acts different when called normally VS. through event handler', 'ViewCount': 113, 'id': 51296018, 'user_AboutMe': None, 'user_AccountId': 7566096.0, 'user_DisplayName': 'dulongj', 'user_Reputation': 307.0, 'user_Views': 145.0, 'user_id': 5741614.0}}