Game crashes after showing intersitial and rewarded ads

Hi, I am using admob for ads. Ads working very well on the Editor but on the phone it doesn’t.
I have a button in the game that show rewarded ads then load next level. And I am showing interstitial ads at end of the level then load next level.
But after loading next level game crashes. I am trying to fix it for days but it keeps happening. I am adding my ad manager script. (unit ids not empty of course)

using System.Collections;
using UnityEngine;
using GoogleMobileAds.Api;
using System;

public class AdManager : MonoBehaviour
{
    public static AdManager instance;
    private BannerView bannerView;
    private RewardedAd rewardedAd;
    private InterstitialAd interstitialAd;

     #if UNITY_ANDROID
        string bannerAdUnitId = " ";
       
        string rewardedAdUnitId = " ";

        string interstitialAdUnitId = " ";

    #else
        string bannerAdUnitId = "unexpected_platform";

        string rewardedAdUnitId = "unexpected_platform";

        string interstitialAdUnitId = "unexpected_platform";

    #endif

    void Awake()
    {
        if(instance != null && instance != this){
            Destroy(this.gameObject);
        }
        else{
            instance = this;
            DontDestroyOnLoad(this.gameObject);
        }
    }

    private void Start() {
        MobileAds.Initialize(initStatus => { });
        RequestInterstitialAd();
        RequestBannerAd();
        RequestRewardedAd();
    }

    //BANNER
    public void RequestBannerAd(){
        if (bannerView != null)
            bannerView.Destroy();
       
        else{
            bannerView = new BannerView(bannerAdUnitId, AdSize.Banner, AdPosition.Bottom);

            AdRequest request = new AdRequest.Builder().Build();

            bannerView.LoadAd(request);
        }
    }

    //REWARDED

    public void RequestRewardedAd(){
        if(rewardedAd != null)
            rewardedAd.Destroy();

        rewardedAd = new RewardedAd(rewardedAdUnitId);

        // Called when an ad request failed to show.
        rewardedAd.OnAdFailedToShow += HandleRewardedAdFailedToShow;
        // Called when the ad is closed.
        rewardedAd.OnAdClosed += HandleRewardedAdClosed;

        AdRequest request = new AdRequest.Builder().Build();

        rewardedAd.LoadAd(request);

    }

    public void ShowRewardedAd(){
        if (rewardedAd.IsLoaded()) {
            rewardedAd.Show();
        }
        else{
            StartCoroutine(RewardedNotLoadedFunctionCall());
        }
    }

    IEnumerator RewardedNotLoadedFunctionCall(){
        FindObjectOfType<GameManagement>().RewardedAdNotLoaded();
        yield return null;
    }

    public void HandleRewardedAdClosed(object sender, EventArgs args)
    {
        RequestRewardedAd();
        StartCoroutine(AdClosedFunctionCall());
    }
    IEnumerator AdClosedFunctionCall(){
        FindObjectOfType<GameManagement>().AdClosed();
        yield return null;
    }

    public void HandleRewardedAdFailedToShow(object sender, AdErrorEventArgs args)
    {
        StartCoroutine(RewardedNotLoadedFunctionCall());
    }

    //interstitial

    private void RequestInterstitialAd(){
        if(interstitialAd != null)
            interstitialAd.Destroy();

        interstitialAd = new InterstitialAd(interstitialAdUnitId);

        interstitialAd.OnAdClosed += HandleOnAdClosed;

        AdRequest request = new AdRequest.Builder().Build();
        interstitialAd.LoadAd(request);
    }

    public void ShowInterstitialAd(){
        if (interstitialAd.IsLoaded()) {
            interstitialAd.Show();
        }
        else{
            StartCoroutine(AdClosedFunctionCall());
        }
    }

    public void HandleOnAdClosed(object sender, EventArgs args)
    {
        RequestInterstitialAd();
        StartCoroutine(AdClosedFunctionCall());
    }
}

Admob Ad does not run on the same thread as Unity’s main thread.

Use UnityMainThreadDispatcher :

public void HandleRewardedAdClosed(object sender, EventArgs args)
{
UnityMainThreadDispatcher.Instance().Enqueue(()=>{
RequestRewardedAd();
StartCoroutine(AdClosedFunctionCall());
});
}

1 Like

Sorry, forgot to update here. I had found UnityMainThreadDispatcher answer and tried it. And it worked, I’ve been using this method ever since.
Anyway, thank you very much for the answer.