0

I am stuck with admob rewarded ads, i can't figurate how to make event working. The problem is that my quiz game reload the scene every questions and even if i prevent the ad from destroy, the event are not firing at all. The ads are showing perfectly. I have tried multiple things but i must make a mistake somewhere... Anyone have an idea ?

Thank you very much!

using System;
using System.Collections;
using UnityEngine;
using GoogleMobileAds.Api;


public class RewardedScriptRow : MonoBehaviour
{
    private RewardBasedVideoAd rewardBasedVideo;
    public AudioClip GiftSound;

    // Use this for initialization
    void Start()
    {    
        RequestInterstitial();
        Debug.Log("Load at start");
    }

    public void LaunchAd() //Called from another script
    {
        StartCoroutine("Load");
    }

    private void RequestInterstitial()
    {

        string adUnitId = "";
#if UNITY_ANDROID
        adUnitId = "ca-app-pub-00000/00000000";
#elif UNITY_IOS
         adUnitId = "ca-app-pub-0000000000000";
#else
         adUnitId = "unexpected_platform";
#endif

        // Get singleton reward based video ad reference.
        this.rewardBasedVideo = RewardBasedVideoAd.Instance;

        // Create an empty ad request.
        AdRequest request = new AdRequest.Builder().Build();
        // Load the interstitial with the request.
        this.rewardBasedVideo.LoadAd(request, adUnitId);

        rewardBasedVideo.OnAdFailedToLoad += HandleRewardBasedVideoClosed;
        rewardBasedVideo.OnAdRewarded += HandleRewardBasedVideoRewarded;
    }


    IEnumerator Load()
    {
        while (!rewardBasedVideo.IsLoaded())
            yield return new WaitForEndOfFrame();

        yield return new WaitForSeconds(0.0f);
        rewardBasedVideo.Show();  
        yield break;
    }



    //EVENT
    public void HandleRewardBasedVideoRewarded(object sender, Reward args)
    {
        GetComponent<AudioSource>().PlayOneShot(GiftSound, 1.0F);
        RequestInterstitial();
    }

    public void HandleRewardBasedVideoClosed(object sender, EventArgs args)
    {
        GetComponent<AudioSource>().PlayOneShot(GiftSound, 1.0F);
        RequestInterstitial();
    }
}

EDIT 1 :

using System;
using System.Collections;
using UnityEngine;
using GoogleMobileAds.Api;


public class RewardedScriptRow : MonoBehaviour
{
    private RewardBasedVideoAd rewardBasedVideo;
    public AudioClip GiftSound;
    public static RewardedScriptRow Instance;

    // Use this for initialization
    void Start()
    {
         Instance = this;
         DontDestroyOnLoad(this);
         RequestRewardBasedVideo();

        rewardBasedVideo.OnAdFailedToLoad += HandleRewardBasedVideoClosed;
        rewardBasedVideo.OnAdRewarded += HandleRewardBasedVideoRewarded;

     }

    //Called after 10 questions
    public void LaunchAd() 
    {
        StartCoroutine("Load");
    }

    private void RequestRewardBasedVideo()
    {

        string adUnitId = "";
#if UNITY_ANDROID
        adUnitId = "ca-app-pub-0000000/0000000000";
#elif UNITY_IOS
         adUnitId = "ca-app-pub-00000/00000000";
#else
         adUnitId = "unexpected_platform";
#endif

        // Get singleton reward based video ad reference.
        this.rewardBasedVideo = RewardBasedVideoAd.Instance;
        // Create an empty ad request.
        AdRequest request = new AdRequest.Builder().Build();
        // Load the interstitial with the request.
        this.rewardBasedVideo.LoadAd(request, adUnitId);

    }

    //EVENT
    public void HandleRewardBasedVideoRewarded(object sender, Reward args)
    {
        GetComponent<AudioSource>().PlayOneShot(GiftSound, 1.0F); 
        RequestRewardBasedVideo();

    }
    public void HandleRewardBasedVideoClosed(object sender, EventArgs args)
    {
        GetComponent<AudioSource>().PlayOneShot(GiftSound, 1.0F);
        RequestRewardBasedVideo();
    }

    IEnumerator Load()
    {
        while (!rewardBasedVideo.IsLoaded())
            yield return new WaitForEndOfFrame();

        yield return new WaitForSeconds(0.0f);
        rewardBasedVideo.Show();
        yield break;
    }
}

And this how the game work with the scenes :

SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);

1 Answers1

0

First of all: It seems very unwise/ illogical to call the RequestInterstial method in your event. Because by doing so, you are creating multiple subscriptions to the same events that you are already subscribed to! This can lead to very undesired/ unwanted behaviour as well as leading to Stackoverflow exceptions

It is unclear to me why you would even call RequestInterstial when the event fires. It seems to me that you would want to load a new video after the first one has been shown. Refactor your method so that you do not add the subscription events.

Move the subscription events and initialization code to your Start or Awake method.

Also you are not requesting an interstial, but a rewardbasedvideo. I'd suggest renaming to keep the code logical.

Public static RewardedScriptRow Instance;

void Start()
{    
    Instance = this;
    DontDestroyOnLoad(this);
    RequestRewardBasedVideo();

    // Get singleton reward based video ad reference.
    this.rewardBasedVideo = RewardBasedVideoAd.Instance;

    rewardBasedVideo.OnAdFailedToLoad += HandleRewardBasedVideoClosed;
    rewardBasedVideo.OnAdRewarded += HandleRewardBasedVideoRewarded;
}

private void RequestRewardBasedVideo()
    {
        #if UNITY_ANDROID
            string appId = "ca-app-pub-3940256099942544~3347511713";
        #elif UNITY_IPHONE
            string appId = "ca-app-pub-3940256099942544~1458002511";
        #else
            string appId = "unexpected_platform";
        #endif

        // Create an empty ad request.
        AdRequest request = new AdRequest.Builder().Build();
        // Load the rewarded video ad with the request.
        this.rewardBasedVideo.LoadAd(request, adUnitId);
    }

public void HandleRewardBasedVideoRewarded(object sender, Reward args)
{
    GetComponent<AudioSource>().PlayOneShot(GiftSound, 1.0F);
    RequestRewardBasedVideo();
}

Other than that, it should work. If you are still not getting the desirable result, try setting breakpoints while debugging and/ or use Debug.Log() inside the subscribed methods to see what's happening.

Edit: Also, if it is happening because of reloading scenes, you could try adding DontDestroyOnLoad(this); to prevent the "AdObject" from getting destroyed. I'd suggest creating this script in your very first scene and removing it from all others (to prevent duplicates).

You can then even apply the singleton pattern so you can easily access the script from within other classes.

Example:

StartCoroutine(RewardedScriptRow.Instance.LaunchAd());
Immorality
  • 2,164
  • 2
  • 12
  • 24
  • Hello ! Thank you so much for your complete answer ! First i modified all the bad things like interstitial name etc... you right. I noticed that the ads is not launching when i put the "this.rewardBasedVideo = RewardBasedVideoAd.Instance;" in the start method, but only when i put it in the request. Otherwise, since i added you code, it seem to work for the reward event but not for the closing ad one, that's weird. Also, after a reward, the ad is not reloading for now, but i'm investigating ! Thank you again :) – Loïc Bernard Sep 05 '18 at 09:40
  • Still stuck since the scene reload itself after each question. I am calling LaunchAd() every 10 question. The first one shows then, the event don't record the reload request at all... The sound is just a debug and it's not playing. So if i quit or just watch the ad, nothing happen at the end of the next row because the ad is not ready but when i quit the scene and relaunch the quiz, the ad appear at the beginning i don't understand all what is going on :D (I edited my answer for more details) – Loïc Bernard Sep 05 '18 at 10:21
  • could it be possible that your Ad has not been loaded yet? – Immorality Sep 05 '18 at 11:10
  • I don't think so, there is around 15 sec between ads – Loïc Bernard Sep 05 '18 at 12:05
  • For the ad who is loading at the beggining it's due to the dont destroy who just duplicate the Ad Object. For the event problem, still searching! – Loïc Bernard Sep 05 '18 at 12:21
  • I told you in my answer, only create the ad Gameobject once. Remove all other instances of it in your other scenes. – Immorality Sep 05 '18 at 13:02
  • I also tried that and no event at all :/ All the debugs are good, ads are working, event call once but no answer from them – Loïc Bernard Sep 05 '18 at 14:23