Search Unity

Admob Rewarded video not calling events after close

Discussion in 'Unity Ads' started by Punicon, Aug 15, 2018.

  1. Punicon

    Punicon

    Joined:
    Jul 12, 2018
    Posts:
    1
    I'm trying to add an Admob reward video ad to my android game made in Unity. The displays fine but when I close the ad, the reward is never given (coin).
    This is my unity c# script.


    -- AdmobRewardedVideo:

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

    public class AdmobRewardedVideo : MonoBehaviour
    {

    private RewardBasedVideoAd rewardBasedVideo;
    public AddCoinScript Controller;

    public void Start()
    {
    DontDestroyOnLoad(gameObject);
    #if UNITY_ANDROID
    string appId = "ca-app-pub-6144446801473222~4916984641";
    #elif UNITY_IPHONE
    string adUnitId = "";
    #else
    string adUnitId = "";
    #endif

    // Initialize the Google Mobile Ads SDK.
    MobileAds.Initialize(appId);

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

    // Called when the user should be rewarded for watching a video.
    rewardBasedVideo_OnAdRewarded += HandleRewardBasedVideoRewarded;

    // Called when the ad is closed.
    rewardBasedVideo_OnAdClosed += HandleRewardBasedVideoClosed;

    this.RequestRewardBasedVideo();
    }

    private void RequestRewardBasedVideo()
    {
    #if UNITY_ANDROID
    //Test;
    string adUnitId = "ca-app-pub-3940256099942544/5224354917";
    #elif UNITY_IPHONE
    string adUnitId = "";
    #else
    string adUnitId = "";
    #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 ShowRewardBasedAd()
    {
    if (rewardBasedVideo.IsLoaded())
    {
    rewardBasedVideo.Show();
    }
    }

    public void HandleRewardBasedVideoRewarded(object sender, Reward args)
    {
    Controller.AddsCoin();
    }

    public void HandleRewardBasedVideoClosed(object sender, EventArgs args)
    {
    this.RequestRewardBasedVideo();
    }
    }


    -- AddCoinScript:

    using UnityEngine;
    using UnityEngine.UI;
    using System.Collections;
    using System;

    public class AddCoinScript : MonoBehaviour
    {

    public Text scoreTextMesh, txtCoin;
    public int coin;
    int numberOfCoins;

    void OnEnable ()
    {
    scoreTextMesh.text = "" + 0;
    numberOfCoins = PlayerPrefs.GetInt("Coin", coin); // Load archive coins
    coin = coin + numberOfCoins;
    txtCoin.text = coin.ToString();
    }

    public void AddsCoin()
    {
    coin += 30;
    txtCoin.text = "" + coin;
    PlayerPrefs.SetInt("Coin", coin); // Archive the Score coins in "PlayerPrefs.SetInt"
    }
    }



    My setup...

    Building for Android (internal)
    Unity 2017.1.Of3
    Google Mobile Ads Plugin
    Windows
     

    Attached Files:

  2. renyangZheng

    renyangZheng

    Joined:
    Oct 11, 2017
    Posts:
    1
    Did you resolve this problem? I have the same issure..
     
  3. StartStart

    StartStart

    Joined:
    Jan 2, 2013
    Posts:
    105
    Is anything error? Did you try logcat it or not.
     
  4. Kailric

    Kailric

    Joined:
    Aug 12, 2015
    Posts:
    15
    Ok, I 'm writing this for others and for future me so if I have issues again there will be a record. There are a few points to remember when coding Admob reward ads.

    1. The reward instance needs to be a singleton placed on an object with DoNotDestory set. In other words the same instance needs to move from scene to scene. I had issues with using the original instance with new callbacks in a new scene. The ad would show but there would be no call backs.

    2. Important: within the call backs, if you add in method calls from another class this can stop the callback from functioning. I am not sure exactly what causes this, but I have been testing this. If I add in calls to other object classes the callback breaks. When I remove them it works.

    So what I do is start a StartCoroutine before the ad is shown that checks for local variables that are set in the callbacks. I have only local variables in the callback.

    To the OP, I see you have calls outside the instance like Controller.AddsCoin(); This maybe what is causing the callbacks to fail. And btw Controller is never assigned within your code. Perhaps you assign it in the editor.

    3. Apparently OnAdRewarded is called after OnAdClosed so you need to be sure that you have your timing setup correct. I learned this from this post here.

    4. Don't forget to use Test Ads as you don't want to get your account in trouble with Google. The reward test ad ID is: ca-app-pub-3940256099942544/5224354917
     
    Last edited: Apr 3, 2019