Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Coroutine Stops after first yield

Discussion in 'Scripting' started by mallani, Nov 23, 2022.

  1. mallani

    mallani

    Joined:
    Dec 24, 2021
    Posts:
    1
    Hi,

    I am experiencing this issue where the code after the first yield in the coroutine is not executed (Method: LoadScene(int scene) )

    Someone has an idea about what I am doing wrong here?

    This is the class which manages my Interstitial Ads. I generated most of the code with Unity mediation. The only change I did was adding the start method and adding the isClosed attribute, which I am using as a flag (true = when ad is show , and false = when ad is closed)

    Code (CSharp):
    1. using System;
    2. using System.Collections;
    3. using System.ComponentModel;
    4. using System.Threading.Tasks;
    5. using Unity.Services.Core;
    6. using Unity.Services.Mediation;
    7. using UnityEngine;
    8.  
    9. public class InterstitialAds : MonoBehaviour, IDisposable
    10. {
    11.     IInterstitialAd ad;
    12.     string adUnitId = "Interstitial_Android";
    13.     string gameId = "XXXXX";
    14.  
    15.     public bool isClosed = true;
    16.  
    17.     void Start()
    18.     {
    19.         #if UNITY_IOS
    20.             adUnitId = "Interstitial _iOS";
    21.             gameId = "XXXXX"
    22.         #elif UNITY_ANDROID
    23.             adUnitId = "Interstitial_Android";
    24.             gameId = "XXXXX";
    25.         #endif
    26.         _ = InitServices();
    27.         isClosed = true;
    28.     }
    29.  
    30.     public async Task InitServices()
    31.     {
    32.         try
    33.         {
    34.             InitializationOptions initializationOptions = new InitializationOptions();
    35.             initializationOptions.SetGameId(gameId);
    36.             await UnityServices.InitializeAsync(initializationOptions);
    37.  
    38.             InitializationComplete();
    39.         }
    40.         catch (Exception e)
    41.         {
    42.             InitializationFailed(e);
    43.         }
    44.     }
    45.  
    46.     public void SetupAd()
    47.     {
    48.         //Create
    49.         ad = MediationService.Instance.CreateInterstitialAd(adUnitId);
    50.  
    51.         //Subscribe to events
    52.         ad.OnClosed += AdClosed;
    53.         ad.OnClicked += AdClicked;
    54.         ad.OnLoaded += AdLoaded;
    55.         ad.OnFailedLoad += AdFailedLoad;
    56.  
    57.         // Impression Event
    58.         MediationService.Instance.ImpressionEventPublisher.OnImpression += ImpressionEvent;
    59.     }
    60.  
    61.     public void Dispose() => ad?.Dispose();
    62.  
    63.  
    64.     public async void ShowAd()
    65.     {
    66.         if (ad.AdState == AdState.Loaded)
    67.         {
    68.             try
    69.             {
    70.                 isClosed = false;
    71.                 Debug.Log("Here. IsClosed: " + isClosed);
    72.                 InterstitialAdShowOptions showOptions = new InterstitialAdShowOptions();
    73.                 showOptions.AutoReload = true;
    74.                 await ad.ShowAsync(showOptions);
    75.                 AdShown();
    76.             }
    77.             catch (ShowFailedException e)
    78.             {
    79.                 isClosed = true;
    80.                 AdFailedShow(e);
    81.             }
    82.             catch (Exception)
    83.             {
    84.                 isClosed = true;
    85.             }
    86.         }
    87.     }
    88.  
    89.     void InitializationComplete()
    90.     {
    91.         SetupAd();
    92.         _ = LoadAd();
    93.     }
    94.  
    95.     async Task LoadAd()
    96.     {
    97.         try
    98.         {
    99.             await ad.LoadAsync();
    100.         }
    101.         catch (LoadFailedException)
    102.         {
    103.             Debug.Log("Loading Ad Failed");
    104.         }
    105.     }
    106.  
    107.     void InitializationFailed(Exception e)
    108.     {
    109.         Debug.Log("Initialization Failed: " + e.Message);
    110.     }
    111.  
    112.     void AdLoaded(object sender, EventArgs e)
    113.     {
    114.         Debug.Log("Ad loaded");
    115.     }
    116.  
    117.     void AdFailedLoad(object sender, LoadErrorEventArgs e)
    118.     {
    119.         Debug.Log("Failed to load ad");
    120.         Debug.Log(e.Message);
    121.     }
    122.  
    123.     void AdShown()
    124.     {
    125.         isClosed = false;
    126.         Debug.Log("Ad shown! closed? : " + isClosed);
    127.     }
    128.  
    129.     void AdClosed(object sender, EventArgs e)
    130.     {
    131.         Debug.Log("Ad has closed");
    132.         isClosed = true;
    133.         // Execute logic after an ad has been closed.
    134.     }
    135.  
    136.     void AdClicked(object sender, EventArgs e)
    137.     {
    138.         Debug.Log("Ad has been clicked");
    139.         isClosed = true;
    140.         // Execute logic after an ad has been clicked.
    141.     }
    142.  
    143.     void AdFailedShow(ShowFailedException e)
    144.     {
    145.         Debug.Log(e.Message);
    146.         isClosed = true;
    147.     }
    148.  
    149.     void ImpressionEvent(object sender, ImpressionEventArgs args)
    150.     {
    151.         var impressionData = args.ImpressionData != null ? JsonUtility.ToJson(args.ImpressionData, true) : "null";
    152.         Debug.Log("Impression event from ad unit id " + args.AdUnitId + " " + impressionData);
    153.     }
    154.  
    155.     public IEnumerator WaitForAdToClose()
    156.     {
    157.         yield return new WaitUntil(() => isClosed);
    158.     }
    159.  
    160. }
    161.  
    I am using this object in another script:

    Code (CSharp):
    1.     public void Quit()
    2.     {
    3.         bannerAd.Hide();
    4.         interstitialAds.ShowAd();
    5.  
    6.         StartCoroutine(LoadScene(0));
    7.     }
    8.  
    9.     IEnumerator LoadScene(int scene)
    10.     {
    11.         yield return new WaitUntil(() => interstitialAds.isClosed);
    12.  
    13.         interstitialAds.Dispose();
    14.  
    15.         Time.timeScale = 1f;
    16.         transition.SetTrigger("fade");
    17.  
    18.         yield return new WaitForSeconds(1);
    19.  
    20.         SceneManager.LoadScene(scene);
    21.     }
    Thanks in advance!
     

    Attached Files:

    StarArcher likes this.
  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,521
    For asking general scripting questions, please use the Scripting forum. The 2D forum isn't appropriate here.

    I'll move your post for you.
     
    mallani likes this.
  3. StarArcher

    StarArcher

    Joined:
    May 23, 2020
    Posts:
    12
    Did you ever get an answer? I'm having a similar problem. SOMETIMES (not always) after the ad runs, control isn't returned so the AdShown() method is never called.