AdMob RewardBasedVideoAd, App crash after reward video closed (Android)

Hi

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 the problem in RewardBasedVideoAd specifically on OnAdRewarded event, when the user closes the video return to the game to earn his reward game crash immediately.

After many tries, I found the code which crashes the game, gameObject.SetActive(true) And gameObject.SetActive(false), is the problem, when I deactivate game panel UI and active reward panel UI game crash immediately.

How can I solve it? why game crash when I use gameObject.SetActive ?


code which make app crashed

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

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;
}

for anyone has this issue

the cause of the problem is TextMesh Pro package

JUST UNINSTALL IT.

Hello there,
I’ve been struggling with this one for weeks now.
I tried various package and unity versions without success.
Removing logic from callbacks or disabling TMP like some people suggested did not seemed to reduce my crash rate at first.

Finally I found out that both of them cause crashes.

So here is my little understanding and workarround for it :
Admob’s package for Unity runs on a separated thread than Unity to pause it while executing the Ad.
Executing logic while in Admob’s thread can have side effects (I was using UnityEvent and Dotween in the callback).
I now set a simple bool is the callbacks when the Ad is done and check it in the Update() loop to execute my logic, to be run by the Unity thread.

Also if you are using Text Mesh Pro, it also seems to crash sometimes when rendering text during the first frame after the Ad.
On a slow phone I observed a short lapse of time where Unity rendered a part of the scene before the crash happened, and the part not rendered was the UI with TMP.
So I deactivated the UI just before the ad is called and activated back 0.1 sec after and it seems to work.

Hope it helps, cheers.

the solution tah work for me was remove Text Mesh Pro

I was having the same issue. Thank you so much for the Information. Adding a coroutine to execute the logic after earning reward worked for me.

//Called on reward earned Event
private void RewardHealthandTime() {

        StartCoroutine(RewardHealthAndTimeToContinue());
 }

private IEnumerator RewardHealthAndTimeToContinue() {

        yield return new WaitForEndOfFrame();
        HealthRewarded = true;
        LivesIndicators.ResetCount();
        if (GameTimer.GetRemaingTimeInSec() < 30)
        {
            GameTimer.AddTime(0.5f);
        }
        gameStats.RefillAllLives();
        
        ResumeGame();
        GameEndUI.DeactivatePanel();

    }

I found the same issue and the fix was to remove all the Textmesh from the ui which which admob script attached.

I had the same issue and in my case i was changing UI and sending requests to server after OnAdClosed and OnRewardEarned and so the crash happens when i close the ad.

I think that the reason is that these changes actually apply before coming back to the app and in that time the app is Paused. So i fixed it by using bool to know that if OnAdClosed and OnRewardEarned are called that then change UI and send requests in the OnApplicationPause function like the code below.

Note: I send analytics events in OnAdClosed and OnRewardEarned and they work fine without any problem or crash.

    private void OnApplicationPause(bool isPaused)
    {
        if (!isPaused)
        {
            if (_isRewardEarned)
            {
                _onEarnedRewardEvent?.Invoke();
            }
            if (_isRewardedAdClosed)
            {
                _onRewardedAdClosedEvent?.Invoke();
                InitializeNextRewardedAd();
            }
        }
    }

None of these answers are correct. I can only hope someone scrolls down this far. AdMob crashes because there is an issue where Unity attempts to run the logic in your scene before the app is back in the foreground. The correct answer on how to manage this is buried in comments on this page: in HandleOnAdClosed() and HandleRewardedAdClosed() do NOT put any logic. Run a coroutine where you yield return new WaitForEndOfFrame() and then insert your logic below that. No need to yield for 0.1s as stated by others, as this is frame rate dependent and you’re probably asking for trouble, though this probably works too.