- Home /
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;
}
Answer by TheCrimsonMoon · Jan 11, 2020 at 10:39 AM
for anyone has this issue
the cause of the problem is TextMesh Pro package
JUST UNINSTALL IT.
I had the same problem. There's some bug in T$$anonymous$$P creating the mesh in the same frame as the app back from background in some cases (I don't know when exactly). There are two ways to avoid it if you don't want to remove T$$anonymous$$P: - Do not make logic in the OnRewardedAdClosed frame, ins$$anonymous$$d of that you can wait one frame or 0.1 seconds. - $$anonymous$$eep it the T$$anonymous$$P gameobject active before showing the ad. You can make several tricks in order to do not show it, but once the gameobject has been activated, you can deactivate and activate again without problems.
Didn't helped for me. One RewardBasedVideoAd works, but the second from other script causes crash
I was also looking for it, never thought of T$$anonymous$$Pro but Im using it. When I used Ienumator and coRoutine, it worked.
That worked for me as well. I used WaitForEndOfFrame() for coroutine. Thanks for the tip.
Answer by cobrecht · Apr 10, 2020 at 05:48 AM
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.
Oh! Thanks man!!!! I was pulling my hair for 5 to 6 days with reward ad behavior in admob events (Specially In on ad closed, No function called, coroutine wasn't working.It's crashed everytime, sometimes show the audio source error.) First I was thinking coroutine won't work in there. After that I prepared normal function and called those in HandleOnadClosed to check if my user force close the ad without watching. U, hu not worked. You were r8 some stupid Admob event's doesn't run in main unity thread, it runs on separate thread. Solved it by only passing some nullable bool in events and doing rest of the work in update function.
bro you mean disable Canvas before reword video and showit after video is done??
Answer by ijeanpierrebp · Apr 04, 2021 at 08:25 PM
the solution tah work for me was remove Text Mesh Pro
Answer by dinesh-dias · Jun 14, 2021 at 08:16 PM
I found the same issue and the fix was to remove all the Textmesh from the ui which which admob script attached.
Answer by SMahdiFaghih · Apr 06 at 11:45 AM
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();
}
}
}