Unity: Admob Reward Video Ad Doesn't Call Events

2,249 views
Skip to first unread message

Zak Woolley

unread,
Apr 8, 2017, 7:39:58 AM4/8/17
to Google Mobile Ads SDK Developers
Hi.
I'm trying to add an Admob reward video ad to my android game made in Unity. The ad displays fine but when I close the ad, the reward is never given. I've tested the code in the function and the works fine so I think the problem is that is isn't getting called. Can anyone help me?

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

    public class textEdit : MonoBehaviour
    {
       public Image lifeAdUI;
       static Image lifeAdUIStat;
       public Text adFailUI;
       static Text adFailUIStat;
       public Button lifeButton;

       private static RewardBasedVideoAd videoAd;
       static bool adTime = false;
       static bool adPlaying = false;
       static int pass = 0;
       bool watched;

      // Use this for initialisation
      void Start()
      {
        Button btn = lifeButton.GetComponent<Button>();
        btn.onClick.AddListener(VideoAd);

        videoAd = RewardBasedVideoAd.Instance;

        videoAd.OnAdFailedToLoad += HandleOnAdFailedToLoad;
        videoAd.OnAdOpening += HandleOnAdOpening;
        videoAd.OnAdClosed += HandleOnAdClosed;
        videoAd.OnAdRewarded += HandleOnAdReward;
        videoAd.OnAdLeavingApplication += HandleOnAdLeavingApplication;
        videoAd.OnAdLoaded += HandleOnAdLoaded;
        videoAd.OnAdStarted += HandleOnAdStarted;

        lifeAdUIStat = lifeAdUI;
        adFailUIStat = adFailUI;

      }


    public static void LoadVideoAd()
    {
    #if UNITY_EDITOR
        string adUnitID = "unused";
    #elif UNITY_ANDROID
        string adUnitID = "ca-app-pub-3025391748532285/9122766975";
    #elif UNITY_IPHONE
        string adUnitID = "";
    #else
        string adUnitID = "unexpected_platform";
    #endif


        videoAd.LoadAd(new AdRequest.Builder().Build(), adUnitID);
        pass = pass + 1;

    }

    void VideoAd()
    {
        if (videoAd.IsLoaded())
        {
            videoAd.Show();


        }
        else
        {
            //ad not loaded
        }
    }

    //Ad Events
    public void HandleOnAdFailedToLoad(object sender, AdFailedToLoadEventArgs args)
    {
        if (pass < 2)
        {
            LoadVideoAd();
        }
        else
        {
            StartCoroutine(adFailCoro());
        }
    }

     public void HandleOnAdOpening(object ssender, EventArgs args)
     {
        adPlaying = true;
     }

     public void HandleOnAdClosed(object sender, EventArgs args)
     {
        adPlaying = false;

        if (watched == true)
        {
            control controlScript = GameObject.FindGameObjectWithTag("Control").GetComponent<control>();

            lifeAdUI.enabled = false;
            StartCoroutine(controlScript.ExtraLife());
        }
     }

 public void HandleOnAdReward(object sender, Reward args)
     {
        watched = true;
     }

     public void HandleOnAdLeavingApplication(object sender, EventArgs args)
     {


     }

      public void HandleOnAdLoaded(object sender, EventArgs args)
      {

      }

      public void HandleOnAdStarted(object sender, EventArgs args)
      {

      }
    }

Ivan Bautista (Mobile Ads SDK Team)

unread,
Apr 10, 2017, 3:37:52 AM4/10/17
to Google Mobile Ads SDK Developers
Hi Zak,

Could you log each Rewarded Ad events and check if they are called accordingly, particularly the OnAdRewarded and OnAdClosed events?

I noticed that the callback method for your OnAdRewarded event doesn't use the value of the returned Reward object. Do you have your own rewarding mechanism locally and you are just using the Rewarded video events as a trigger to when your local rewarding logic will be called? If that's the case, then this could be an implementation issue because I've verified on my end that the OnAdRewarded and OnAdClosed are being called using the sample app accordingly. 

To help us investigate further, please provide us a miniature sample project where issue is reproducible.

Regards,
Ivan Bautista
Mobile Ads SDK Team

Flash...@live.com

unread,
Apr 10, 2017, 4:47:35 AM4/10/17
to Google Mobile Ads SDK Developers
I can confirm that I'm having the exact same issue.
In Addition: Log-Messages are not being send, so it seems they don't get called.
Non-Rewarded Interstitials work as expected in the very same Project.
I will check if they do get called in the sample app build and run on my devices.

Zak Woolley

unread,
Apr 10, 2017, 3:38:31 PM4/10/17
to Google Mobile Ads SDK Developers
Hi. I've uploaded my unity project to dropbox. I've set it to one life so if you wait for a few seconds a game over screen should appear. That's when the ad is requested. Then a green button appears which when pressed, shows the ad. What should happen when the user is rewarded is a 1-up screen should appear and the game will get back to it however, nothing happens.


Thanks,
Zak

Ivan Bautista (Mobile Ads SDK Team)

unread,
Apr 11, 2017, 3:34:26 AM4/11/17
to Google Mobile Ads SDK Developers
Hi Zak,

Thank you for providing us a sample project. I tried to run the app and followed the replication steps you've specified. After the end of the Rewarded video, the "FindGameObjectWithTag can only be called from the main thread." error was thrown instead. Below is a more detailed trace of the error log:

FindGameObjectWithTag can only be called from the main thread.
Constructors and field initializers will be executed from the loading thread when loading a scene.
Don't use this function in the constructor or field initializers, instead move initialization code to the Awake or Start function.
UnityEngine.GameObject:FindGameObjectWithTag(String)
textEdit:OnAdRewarded(Object, Reward) (at /Users/ivanpa/Downloads/minigames/Assets/textEdit.cs:333)

Looking at the nature of the error, this seems to be more of an implementation issue rather than with the SDK because the onAdRewarded event was called accordingly after I comment out the code inside onAdRewarded() in textEdit.cs. In your scenario, you may try to transfer your rewarding logic from the onAdRewarded block to the Update() method and just set a flag in the onAdRewarded method so that in the next call of Update(), your rewarding logic will run accordingly. Similar to something like the pseudo code below:

bool rewarded = false; 
onAdRewarded(...){ rewarded = true; } 
void Update() { if (rewarded){ ---- do your rewarding logic here instead ---- rewarded = false; } }

However, being that we only handle concerns related specifically to Mobile Ads SDK or the Unity plugin, I suggest that you raise this concern to the Unity Community forums to get more accurate solutions on how to handle this error.

Zak Woolley

unread,
Apr 11, 2017, 2:54:38 PM4/11/17
to Google Mobile Ads SDK Developers
Hi. This worked. Thanks for all your help.

Thanks,
Zak

Sherzod Rashidov

unread,
May 28, 2017, 3:15:19 PM5/28/17
to Google Mobile Ads SDK Developers
Hi Zak,

Could you please share you Admob script which worked for you, I am trying to call events to but it is not calling?

Thank you in advance.

Hazem Ahmed

unread,
Nov 14, 2017, 3:36:20 PM11/14/17
to Google Mobile Ads SDK Developers
Hey zak i have the same issue can you please tell me how you fixed it ? 

Joshua Lagonera (Mobile Ads SDK Team)

unread,
Nov 14, 2017, 9:36:54 PM11/14/17
to Google Mobile Ads SDK Developers
HI Hazem,

Kindly refrain from reopening old threads. If you would like further assistance on anything specific to the Mobile Ads SDK and its implementation, then please create a new thread further describing your issue and provide to us helpful information, such as your code snippets, and we will be glad to assist you thereon.

Regards,
Joshua Lagonera
Mobile Ads SDK Team
Reply all
Reply to author
Forward
0 new messages