Search code examples
c#unity-game-enginevideoadmobrewardedvideoad

Unity Admob Rewarded Video Listeners Not Working after changing Scene


This problem has struck me recently, never noticed it before, apparently, i have used admob rewarded Video in my Unity3D project.

In My Project I reward the User on Level Fail, when user watches the Reward Video a reward is Given to User and Scene Changes Automatically to Level Selection. When the User starts gameplay again, and fails again the watches the video, but nothing happens.

After some debugging i found the listeners were not working the second time the gameplay scene was loaded.

I have a GamePlayManager Script where i am doing all the functionalities..

public class GamePlayManager : MonoBehaviour {
private RewardBasedVideoAd rewardBasedVideoAd;

void Start()
{
rewardBasedVideoAd = RewardBasedVideoAd.Instance;
        RequestRewardBasedVideo ();

        rewardBasedVideoAd.OnAdFailedToLoad += HandleRewardBasedVideoFailedToLoad;
        // has rewarded the user.
        rewardBasedVideoAd.OnAdRewarded += HandleRewardBasedVideoRewarded;
}

private void RequestRewardBasedVideo()
    {

        #if UNITY_ANDROID
        string adUnitId = GameConstants.AdmobRewardedVideoID;
        #elif UNITY_IPHONE
        string adUnitId = GameConstants.AdmobIOSRewardedVideoID;
        #else
        string adUnitId = "unexpected_platform";
        #endif

        AdRequest request = new AdRequest.Builder().Build();
        rewardBasedVideoAd.LoadAd(request, adUnitId);
    }

    public void showAdmobRewardedVideo()
    {
        if (rewardBasedVideoAd.IsLoaded())
        {
            rewardBasedVideoAd.Show();
        }
    }

    public void HandleRewardBasedVideoRewarded(object sender, Reward args)
    {

        Debug.Log ("Unity AdmobRewardGiven");

        nextLevel ();
        gotoLevelSelection ();

    }


    public void HandleRewardBasedVideoFailedToLoad(object sender, AdFailedToLoadEventArgs args)
    {
        Debug.Log ("Admob RewardedVideo FailedToLoad");
    }
}

Solution

  • When you load new scene GamePlayManager is destroyed. You have two options:

    1.Add DontDestroyOnLoad(gameObject); to the Start function so that the GamePlayManager script is not destroyed when new scene is loaded.

    2.Unsubscribe to the events in the OnDestroy function with -= just like you subscribe with +=. I don't see the Admob examples doing this but you should always unsubscribe from your events.

    public void OnDestroy()
    {
        rewardBasedVideoAd.OnAdFailedToLoad -= HandleRewardBasedVideoFailedToLoad;
    
        rewardBasedVideoAd.OnAdRewarded -= HandleRewardBasedVideoRewarded;
    }
    

    Any of these should fix your issue. If one fails, use the other one.