'AdMob RewardBasedVideoAd, App crash 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 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;
}
Solution 1:[1]
for anyone has this issue
the cause of the problem is TextMesh Pro package
JUST UNINSTALL IT.
Solution 2:[2]
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 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();
}
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | TheDarkLight |
| Solution 2 | Seyyed Mahdi Faghih |
