AdMob RewardBasedVideoAd app crashing after reward video closed (Unity3dd, Android)

I’m working on an Android game and I want to add AdMob ads to my game. I have added Banner and interstitial Views, but I’m having an issue with the RewardBasedVideoAd specifically on the OnAdRewarded event. When the user closes the video and returns to the game to earn their reward, the game crashes immediately.

After many tries, I found the problem to be with the code gameObject.SetActive(true) and gameObject.SetActive(false). When I deactivate the game panel UI and activate the reward panel UI, the game crashes immediately.

How can I solve this issue? Why does the game crash when I use gameObject.SetActive?

Request reward code

public void RequestReward()
{
   AdRequest request = new AdRequest().Builder().Build();
   this.rewardAd.LoadAd(request, rewardAdId);

   rewardAd.OnAdLoaded += this.HandleOnRewardAdLoaded;
   rewardAd.OnAdRewarded += this.HandleOnAdRewarded;
   rewardAd.OnAdClosed += this.HandleOnRewardAdClosed;
}

Handlers

public void HandleOnRewardAdLoaded(object sender, EventArgs args)
{
   if(rewardAd.IsLoaded())
   {
       rewardAd.Show();
   }
}

public void HandleOnAdRewarded(object sender, EventArgs args)
{
    gamePanel.SetActive(false);
    rewardPanel.SetActive(true);
}

public HandleOnRewardAdClosed(object sender, EventArgs args)
{
    rewardAd.OnAdLoaded -= this.HandleOnRewardAdLoaded;
    rewardAd.OnAdRewarded -= this.HandleOnAdRewarded;
    rewardAd.OnAdClosed -= this.HandleOnRewardAdClosed;
}

I’m having an issue with the RewardBasedVideoAd in my Android game. When the user closes the video and returns to the game to earn their reward, the game crashes. I have identified the problem to be with the code gameObject.SetActive(true) and gameObject.SetActive(false) when deactivating the game panel UI and activating the reward panel UI.

How can I solve this issue? Why does the game crash when I use gameObject.SetActive?

Request reward code

public void RequestReward()
{
   AdRequest request = new AdRequest().Builder().Build();
   this.rewardAd.LoadAd(request, rewardAdId);

   rewardAd.OnAdLoaded += this.HandleOnRewardAdLoaded;
   rewardAd.OnAdRewarded += this.HandleOnAdRewarded;
   rewardAd.OnAdClosed += this.HandleOnRewardAdClosed;
}

Handlers

public void HandleOnRewardAdLoaded(object sender, EventArgs args)
{
   if(rewardAd.IsLoaded())
   {
       rewardAd.Show();
   }
}

public void HandleOnAdRewarded(object sender, EventArgs args)
{
    gamePanel.SetActive(false);
    rewardPanel.SetActive(true);
}

public HandleOnRewardAdClosed(object sender, EventArgs args)
{
    rewardAd.OnAdLoaded -= this.HandleOnRewardAdLoaded;
    rewardAd.OnAdRewarded -= this.HandleOnAdRewarded;
    rewardAd.OnAdClosed -= this.HandleOnRewardAdClosed;
}

I’m having an issue with the RewardBasedVideoAd in my Android game. When the user closes the video and returns to the game to earn their reward, the game crashes. I have identified the problem to be when deactivating the game panel UI and activating the reward panel UI using gameObject.SetActive(true) and gameObject.SetActive(false).

How can I solve this issue? Why does the game crash when I use gameObject.SetActive?

Request reward code

public void RequestReward()
{
   AdRequest request = new AdRequest().Builder().Build();
   this.rewardAd.LoadAd(request, rewardAdId);

   rewardAd.OnAdLoaded += this.HandleOnRewardAdLoaded;
   rewardAd.OnAdRewarded += this.HandleOnAdRewarded;
   rewardAd.OnAdClosed += this.HandleOnRewardAdClosed;
}

Handlers

public void HandleOnRewardAdLoaded(object sender, EventArgs args)
{
   if(rewardAd.IsLoaded())
   {
       rewardAd.Show();
   }
}

public void HandleOnAdRewarded(object sender, EventArgs args)
{
    gamePanel.SetActive(false);
    rewardPanel.SetActive(true);
}

public HandleOnRewardAdClosed(object sender, EventArgs args)
{
    rewardAd.OnAdLoaded -= this.HandleOnRewardAdLoaded;
    rewardAd.OnAdRewarded -= this.HandleOnAdRewarded;
    rewardAd.OnAdClosed -= this.HandleOnRewardAdClosed;
}

I’m having an issue with the RewardBasedVideoAd in my Android game. When the user closes the video and returns to the game to earn their reward, the game crashes. I have identified the problem to be when deactivating the game panel UI and activating the reward panel UI using gameObject.SetActive(true) and gameObject.SetActive(false).

How can I solve this issue? Why does the game crash when I use gameObject.SetActive?

Request reward code

public void RequestReward()
{
   AdRequest request = new AdRequest().Builder().Build();
   this.rewardAd.LoadAd(request, rewardAdId);

   rewardAd.OnAdLoaded += this.HandleOnRewardAdLoaded;
   rewardAd.OnAdRewarded += this.HandleOnAdRewarded;
   rewardAd.OnAdClosed += this.HandleOnRewardAdClosed;
}

Handlers

public void HandleOnRewardAdLoaded(object sender, EventArgs args)
{
   if(rewardAd.IsLoaded())
   {
       rewardAd.Show();
   }
}

public void HandleOnAdRewarded(object sender, EventArgs args)
{
    gamePanel.SetActive(false);
    rewardPanel.SetActive(true);
}

public HandleOnRewardAdClosed(object sender, EventArgs args)
{
    rewardAd.OnAdLoaded -= this.HandleOnRewardAdLoaded;
    rewardAd.OnAdRewarded -= this.HandleOnAdRewarded;
    rewardAd.OnAdClosed -= this.HandleOnRewardAdClosed;
}

I’m having an issue with the RewardBasedVideoAd in my Android game. When the user closes the video and returns to the game to earn their reward, the game crashes. I have identified the problem to be when activating the reward panel UI using gameObject.SetActive(true) and gameObject.SetActive(false).

How can I solve this issue? Why does the game crash when I use gameObject.SetActive?

Request reward code

public void RequestReward()
{
   AdRequest request = new AdRequest().Builder().Build();
   this.rewardAd.LoadAd(request, rewardAdId);

   rewardAd.OnAdLoaded += this.HandleOnRewardAdLoaded;
   rewardAd.OnAdRewarded += this.HandleOnAdRewarded;
   rewardAd.OnAdClosed += this.HandleOnRewardAdClosed;
}

Handlers

public void HandleOnRewardAdLoaded(object sender, EventArgs args)
{
   if(rewardAd.IsLoaded())
   {
       rewardAd.Show();
   }
}

public void HandleOnAdRewarded(object sender, EventArgs args)
{
    gamePanel.SetActive(false);
    rewardPanel.SetActive(true);
}

public HandleOnRewardAdClosed(object sender, EventArgs args)
{
    rewardAd.OnAdLoaded -= this.HandleOnRewardAdLoaded;
    rewardAd.OnAdRewarded -= this.HandleOnAdRewarded;
    rewardAd.OnAdCl

The game crashes when using gameObject.SetActive because the UI elements are being modified from a non-UI thread. To solve this issue, you can wrap the code in the handlers with UnityMainThreadDispatcher.Instance().Enqueue() to ensure that the UI changes are made on the main thread.

Here’s an updated version of the code:

using UnityEngine;
using UnityEngine.UI;
using System;
using GoogleMobileAds.Api;

public class AdManager : MonoBehaviour
{
    public RewardBasedVideoAd rewardAd;
    public GameObject gamePanel;
    public GameObject rewardPanel;
    public string rewardAdId = "your_reward_ad_id_here";

    void Start()
    {
        rewardAd = RewardBasedVideoAd.Instance;

        rewardAd.OnAdLoaded += HandleOnRewardAdLoaded;
        rewardAd.OnAdRewarded += HandleOnAdRewarded;
        rewardAd.OnAdClosed += HandleOnRewardAdClosed;
    }

    public void RequestReward()
    {
        AdRequest request = new AdRequest.Builder().Build();
        rewardAd.LoadAd(request, rewardAdId);
    }

    public void ShowReward()
    {
        if (rewardAd.IsLoaded())
        {
            rewardAd.Show();
        }
        else
        {
            Debug.Log("Reward ad is not ready yet");
        }
    }

    public void HandleOnRewardAdLoaded(object sender, EventArgs args)
    {
        if (rewardAd.IsLoaded())
        {
            ShowReward();
        }
    }

    public void HandleOnAdRewarded(object sender, EventArgs args)
    {
        UnityMainThreadDispatcher.Instance().Enqueue(() => {
            gamePanel.SetActive(false);
            rewardPanel.SetActive(true);
        });
    }

    public void HandleOnRewardAdClosed(object sender, EventArgs args)
    {
        rewardAd.OnAdLoaded -= HandleOnRewardAdLoaded;
        rewardAd.OnAdRewarded -= HandleOnAdRewarded;
        rewardAd.OnAdClosed -= HandleOnRewardAdClosed;
    }
}