Search Unity

Coroutine crashes app when run from HandleRewardBasedVideoRewarded (Unity AdMob Plugin)

Discussion in 'Scripting' started by jeremydulong, Jul 11, 2018.

  1. jeremydulong

    jeremydulong

    Joined:
    Nov 9, 2017
    Posts:
    11
    I'm not sure if this is the right place to post but I tried stack overflow and got no replies.
    I am trying to run the noAdsReviveCountdown() coroutine as a reward for the player finishing a rewarded video ad but it keeps crashing the app when it hits the call. Commenting out this call causes the video ad works as expected.

    Logs from Xcode just give me "libc++abi.dylib: terminating with uncaught exception of type Il2CppExceptionWrapper"

    Can I not run a coroutine in the reward handler? I've tried commenting out various parts of the function but it seems even the bare minimum of the coroutine crashes the app.

    Code (CSharp):
    1. // No Ad Revive Countdown function - after 3 seconds the game will resume
    2. public IEnumerator noAdsReviveCountdown() {
    3.  
    4.    continueButton.interactable = false;
    5.    continueText.gameObject.SetActive (false);
    6.    adReviveButton.gameObject.SetActive (false);
    7.    noAdsReviveCountdownText.gameObject.SetActive (true);
    8.  
    9.    timeLeft = 3;
    10.    while (timeLeft >= 0) {
    11.        noAdsReviveCountdownText.text = timeLeft.ToString();
    12.        noAdsReviveCountdownAnim.ResetTrigger ("CountdownTrigger");
    13.        noAdsReviveCountdownAnim.SetTrigger ("CountdownTrigger");
    14.        yield return new WaitForSecondsRealtime(1.0f);
    15.        timeLeft--;
    16.    }
    17.    if (timeLeft < 0) {
    18.        var balls = GameObject.FindGameObjectsWithTag ("ball");
    19.        foreach (var ball in balls) {
    20.            Destroy (ball);
    21.        }
    22.        noAdsReviveCountdownText.gameObject.SetActive (false);
    23.        continueButton.gameObject.SetActive (false);
    24.        Time.timeScale = 1;
    25.    }
    26.  
    27. }
    This coroutine is called here in a separate script that handles all of my AdMob functions:
    Code (CSharp):
    1.     public void HandleRewardBasedVideoClosed(object sender, EventArgs args){
    2.         MonoBehaviour.print("HandleRewardBasedVideoClosed event received");
    3.         if (reward)
    4.             StartCoroutine ( gm.noAdsReviveCountdown() );
    5.         else
    6.             SceneManager.LoadScene (SceneManager.GetActiveScene ().name);
    7.     }
    8.  
    9.     public void HandleRewardBasedVideoRewarded(object sender, Reward args) {
    10.         MonoBehaviour.print( "HandleRewardBasedVideoRewarded event received");
    11.         reward = true;
    12.  
    13.     }
    For more background information, I'm calling the same coroutine in another piece of my code and it works perfectly. This is the case when the user will not see an ad, but skip right to running the coroutine.
    Code (CSharp):
    1.     // Show user a rewarded ad video
    2.     // If the user has paid to remove ads, reward the player without an ad
    3.     public void reviveButtonPressed() {
    4.  
    5.         if (firstLife == true && PlayerPrefs.GetString ("Ads") == "HasAds") {
    6.             gameObject.GetComponent<GoogleMobileAdsBannerScript> ().ShowRewardBasedVideo ();
    7.             firstLife = false;
    8.         } else if (firstLife == true && PlayerPrefs.GetString("Ads") == "NoAds") {
    9.             StartCoroutine ( noAdsReviveCountdown() );
    10.             firstLife = false;
    11.         }
    12.  
    13.     }
    This, coupled with the fact that the ad handler only crashes the app when the coroutine is called, makes me think that the handler somehow cannot run the function properly. But I don't know why.
     
    Last edited: Jul 11, 2018
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,187
    Use code tags and show the rest of the script if you can (if it's to large, include the class and where you call the coroutine from and your event, stuff that is relevant to the methods).
     
    jeremydulong likes this.
  3. jeremydulong

    jeremydulong

    Joined:
    Nov 9, 2017
    Posts:
    11
    Thanks for answering Brathnann, I've cleaned up the post and added the handler methods that call the coroutine.
     
    Last edited: Jul 11, 2018
  4. jeremydulong

    jeremydulong

    Joined:
    Nov 9, 2017
    Posts:
    11
    It was a simple mistake after all. The AdMob script was not referencing the gm script to call the coroutine properly. Oops :)
     
  5. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,187
    Glad you were able to get it figured out. :)
     
  6. FlowORemi

    FlowORemi

    Joined:
    Apr 26, 2019
    Posts:
    7
    Code (CSharp):
    1.  
    2.  
    3. bool paused = false;
    4. void OnApplicationPause(bool pauseStatus)
    5. {
    6.     paused = pauseStatus;
    7. }
    8.  
    9. public IEnumerator noAdsReviveCountdown() {
    10.     //Add this line.
    11.      while (paused)
    12.      {
    13.           yield return null;
    14.      }
    15.  
    16.     continueButton.interactable = false;
    17.     continueText.gameObject.SetActive (false);
    18.     adReviveButton.gameObject.SetActive (false);
    19.     noAdsReviveCountdownText.gameObject.SetActive (true);
    20.     timeLeft = 3;
    21.     while (timeLeft >= 0) {
    22.         noAdsReviveCountdownText.text = timeLeft.ToString();
    23.         noAdsReviveCountdownAnim.ResetTrigger ("CountdownTrigger");
    24.         noAdsReviveCountdownAnim.SetTrigger ("CountdownTrigger");
    25.         yield return new WaitForSecondsRealtime(1.0f);
    26.         timeLeft--;
    27.     }
    28.     if (timeLeft < 0) {
    29.         var balls = GameObject.FindGameObjectsWithTag ("ball");
    30.         foreach (var ball in balls) {
    31.             Destroy (ball);
    32.         }
    33.         noAdsReviveCountdownText.gameObject.SetActive (false);
    34.         continueButton.gameObject.SetActive (false);
    35.         Time.timeScale = 1;
    36.     }
    37. }
    38.  
    Pause states was my problem when running coroutine in OnEarnedReward()