Admob stops working at changing scenes (Unity 2D)

466 views
Skip to first unread message

Darius David

unread,
Aug 12, 2020, 9:10:39 AM8/12/20
to Google Mobile Ads SDK Developers
Hello guys. My game worked perfectly until implementing the rewarded video ads from Admob. The first time I play the game everything is nice, I can die and get an extra life for watching a video (3 chances).
The problem is when I am no longer able to watch videos to gain the extra life, I have to press a button "Ok" that will re-load the current scene. When reloading the current scene, the adlisteners events stop working. I am able to play again, to die, and to watch the video for the reward, but after closing the video I don't get my reward, so it seems there is a problem with the listeners. Could you help me identify it? I've been struggling for almost a week and checked all the tutorials and topics from this site but neither one seems to work.. here is my ads script (where I removed the parts with banner and interstitial because those work as expected)
using System;
using UnityEngine;
using UnityEngine.UI;
using GoogleMobileAds.Api;
public class adsScript : MonoBehaviour
{
    public static adsScript instance {get; private set;}
    private void Awake()
    {
        if (instance == null) {instance = this; DontDestroyOnLoad(this.gameObject);}
        else { Destroy(this.gameObject); }
    } 
    private RewardedAd rewardedAd;
    public void RequestReward()
    {
        string adUnitId = "ca-app-pub-3940256099942544/5224354917";
        this.rewardedAd = new RewardedAd(adUnitId);

        this.rewardedAd.OnAdLoaded += HandleRewardedAdLoaded;
        this.rewardedAd.OnAdFailedToLoad += HandleRewardedAdFailedToLoad;
        this.rewardedAd.OnAdOpening += HandleRewardedAdOpening;
        this.rewardedAd.OnAdFailedToShow += HandleRewardedAdFailedToShow;
        this.rewardedAd.OnUserEarnedReward += HandleUserEarnedReward;
        this.rewardedAd.OnAdClosed += HandleRewardedAdClosed;

        AdRequest request = new AdRequest.Builder().Build();
        this.rewardedAd.LoadAd(request);
    }
    public GameManager gameManager;

    public void HandleRewardedAdLoaded(object sender, EventArgs args)
    {
        MonoBehaviour.print("HandleRewardedAdLoaded event received");
    }
    public void HandleRewardedAdFailedToLoad(object sender, AdErrorEventArgs args)
    {
        MonoBehaviour.print(
            "HandleRewardedAdFailedToLoad event received with message: " + args.Message);
        this.reqRewardedAdVideo = true;
    }
    public void HandleRewardedAdOpening(object sender, EventArgs args)
    {
        MonoBehaviour.print("HandleRewardedAdOpening event received");
    }
    public void HandleRewardedAdFailedToShow(object sender, AdErrorEventArgs args)
    {
        MonoBehaviour.print(
            "HandleRewardedAdFailedToShow event received with message: " + args.Message);
    }
    public void HandleRewardedAdClosed(object sender, EventArgs args)
    {
        MonoBehaviour.print("HandleRewardedAdClosed event received");
        this.reqRewardedAdVideo = true;
        OnDestroy();
    }
    public void HandleUserEarnedReward(object sender, Reward args)
    {
        gameManager.gameOverCanvas.SetActive(false);
        gameManager.ShowReward();
    }
    public void ShowRewardVideo()
    {
        if (this.rewardedAd.IsLoaded())
        {
            this.rewardedAd.Show();
        }
        else
        {
            Debug.Log("Reward video is not ready yet");
        }
    }
    public void Start()
    {
        MobileAds.Initialize(initStatus => { });
        this.RequestBanner();
        this.RequestInterstitial();
        this.RequestReward();
    }

    private bool reqRewardedAdVideo = true;
    public void Update()
    {
        if (reqRewardedAdVideo)
        {
            this.RequestReward();
            reqRewardedAdVideo = false;
        }
    }
    public void OnDestroy()
    {
        this.rewardedAd.OnAdLoaded -= HandleRewardedAdLoaded;
        this.rewardedAd.OnAdFailedToLoad -= HandleRewardedAdFailedToLoad;
        this.rewardedAd.OnAdOpening -= HandleRewardedAdOpening;
        this.rewardedAd.OnAdFailedToShow -= HandleRewardedAdFailedToShow;
        this.rewardedAd.OnUserEarnedReward -= HandleUserEarnedReward;
        this.rewardedAd.OnAdClosed -= HandleRewardedAdClosed;
    }
}
Any suggestion is welcome, as I tried to do absolutely all the things I could find on internet 

Mobile Ads SDK Forum Advisor Prod

unread,
Aug 12, 2020, 11:05:12 AM8/12/20
to darius...@gmail.com, google-adm...@googlegroups.com
Hi Darius,

Thank you for bringing this to our attention.

First, I would ask if it would be possible to get a little darker print of the code. As what I am seeing is very light and hard to read.

As for what I read. It would appear that you have the SDK properly installed and working. As you state that you are able to get it to work 3 times as you require. But when doing it a 4th time would seem to break something within your code. 

My initial thoughts would be that there is something in the code that is causing the adListener to break. Are you handling any logic with the death that like the person can only use it 3 or 4 times? As you stated that it worked fine 3 times before. So, I would suggest to share with us via "Reply privately to author" button. With a sample app showing the implementation and code being executed.

Regards,
William Pescherine
Mobile Ads SDK Team

ref:_00D1U1174p._5004Q23Jaq6:ref

Darius David

unread,
Aug 12, 2020, 7:44:48 PM8/12/20
to Google Mobile Ads SDK Developers
Oh, I'm very sorry for the code, idk why it pasted like this. I've written it here  https://pastebin.com/uADw0ybz  . And the only function that is not there is ShowReward, from GameManager script, that allows me open the panel with the extra-life:
public void ShowReward() 
birdParent.SetActive(false); 
afterReward = 2; 
gameOverCanvas.SetActive(false); 
score.SetActive(false); 
rewardPanel.SetActive(true); 
} 
It's not a problem with the number of ads displayed for gaining the extra-life (the reward), it could be 3 or it could be 400.. I've now deleted that restriction and you could watch an ad every time you die, so you are no longer limited on gaining rewards but the problem is when (let's say) you watched 250 ads and then you click a button 'Ok' to start the game again, from 0 score. That moment, unity has to reload the current scene (with the game) again and boom, here the things get messed up.
I will provide some photos, maybe you could understand better what I'm trying to say.
1.png
so, this is the first time I die. I click the upper button 'Continue', I finish watching the video and I get a reward panel that says "Congrats, here is your extra life. Press Ok". ->2.png
I press Ok and then the game continues from the point where I died, as expected. After doing this thing 3 times (or any times, as I said, because not the number is the problem), the single solution I have to play again is to press the 'OK' or the 'MENU' button, both of them loading a scene. (OK loading the same scene and MENU loading the 'Menu' scene)
3.png
No matter which one I choose, something bad happens. I restart the game and then I die. ->
4.png
I have the button for continuing, I have the ad, I can view the ad but after closing it, I don't get the reward panel as before, it simply remains at this game over panel and it allows me watch 1500 video ads :)) but not rewarding me at all :(

about the "Reply privately to author" button.. sorry but I can't access it. it is blocked somehow, couldn't click it

Mobile Ads SDK Forum Advisor Prod

unread,
Aug 12, 2020, 10:02:14 PM8/12/20
to darius...@gmail.com, google-adm...@googlegroups.com

Hi Darius,

Thank you for expanding on your concerns.

I suspect that the action of restarting your game level may be incorrectly tampering with your ads script (especially with the singleton vs OnDestroy() interaction), although I'm afraid that I am unable to pinpoint the main cause of the issue just yet. Just to double check the implementation: is your game manager script also a singleton that is still properly referenced by your ads script after a level restart?

With all this in mind, could you kindly provide the details below (via a private email to mobileads...@gmail.com) so that I can further investigate the issue?

  • Copy of a sample project (replicating the issue)
  • Steps to reproduce the issue


You may zip the entire Unity project (excluding the Library and Temp folders) in order to reduce its size, then send it to me via a shareable link from your Google Drive (or on any other file-sharing site that you prefer). Don't forget to send the shareable link via a private email to mobileads...@gmail.com.

Regards,
Ziv Yves Sanchez
Mobile Ads SDK Team



ref:_00D1U1174p._5004Q23Jaq6:ref
Reply all
Reply to author
Forward
0 new messages