Azhar Waheed
Azhar Waheed

Reputation: 1

Why isn't my code increasing the score on reward video ad watched?

When I click a button to load a reward video ad the score should double. Instead the reward ad plays and when I close after watching it, nothing happens. I am using public static variable points to get the score value from the in game scene to the level finished scene, and then I get the total score from player prefabs. I am new to coding.

Bellow is code.

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

public class Admob : MonoBehaviour
{

    private BannerView adBanner;
    private RewardBasedVideoAd adReward;
    int pointts;
    int Totalpointts;
    private string idApp, idBanner, idReward;
    bool saveit = false;
    float Timer;
    public Text TotalPointsText;
    public GameObject congratulationspop;
    //public WatchVideo x2coinsWatch;

    [SerializeField] Button BtnX2;

    // Start is called before the first frame update
    [Obsolete]
    void Start()
    {
        //get data
        //
        idApp = "ca-app-pub-3940256099942544~3347511713";
        idBanner = "ca-app-pub-3940256099942544/6300978111";
        idReward = "ca-app-pub-3940256099942544/5224354917";

        adReward = RewardBasedVideoAd.Instance;
        MobileAds.Initialize(idApp);

        Debug.LogWarning("Banner Ad Embeded");
        RequestBannerAd();
    }

    // Update is called once per frame
    void Update()
    {

    }

    public void GiveRewards()
    {
        BtnX2.interactable = false;
        BtnX2.GetComponentInChildren<Text>().text = "Loading...";
        RequestRewardAd();
    }

    public void RequestBannerAd()
    {
        adBanner = new BannerView(idBanner, AdSize.Banner, AdPosition.Bottom);
        AdRequest request = AdRequestBuild();
        adBanner.LoadAd(request);
    }

    public void DestroyBannerAd()
   {
        if (adBanner != null)
        {
        adBanner.Destroy();
        }
   }

    public void RequestRewardAd()
    {
        AdRequest request = AdRequestBuild();
        adReward.LoadAd(request, idReward);

        adReward.OnAdLoaded += this.HandleOnRewardedAdLoaded;
        adReward.OnAdRewarded += this.HandleOnRewarded;
        adReward.OnAdClosed += this.HandleOnRewardedAdClosed;
    }

    public void ShowRewardAd()
    {
        if (adReward.IsLoaded())
        adReward.Show();
    }

    public void HandleOnRewardedAdLoaded(object sender, EventArgs args) // Ad Loaded
    {
        ShowRewardAd();
    }

    public void HandleOnRewardedAdOpening(object sender, EventArgs args) // Ad Opening
    {

    }

    public void HandleOnRewarded(object sender, EventArgs args) // User Finished Wacthing The Ad
    {
        // Funtion here to double the coins
        x2Thepoints();
    }

    public void HandleOnRewardedAdClosed(object sender, EventArgs args) // ad closed not watching the 
ad
    {
        BtnX2.interactable = false;
        BtnX2.GetComponentInChildren<Text>().text = "X2 Points";
        //testing

        //testing till here
        adReward.OnAdLoaded -= this.HandleOnRewardedAdLoaded;
        adReward.OnAdRewarded -= this.HandleOnRewarded;
        adReward.OnAdClosed -= this.HandleOnRewardedAdClosed;
    }

    AdRequest AdRequestBuild()
    {
        return new AdRequest.Builder().Build();
    }

    void OnDestroy()
    {
         DestroyBannerAd();

        adReward.OnAdLoaded -= this.HandleOnRewardedAdLoaded;
        adReward.OnAdRewarded -= this.HandleOnRewarded;
        adReward.OnAdClosed -= this.HandleOnRewardedAdClosed;
    }

    private void FixedUpdate()
    { 
        Totalpointts = PlayerPrefs.GetInt("points" , Totalpointts);
        TotalPointsText.text = "Total Points : " + Totalpointts;
        pointts = destination.Mypoint;

        Debug.Log(pointts);
        if (saveit == true)
        {
            Timer += Time.deltaTime;
            if (Timer >= 2f)
            {
                Points.inc.OnSavePoint();
                saveit = false;
            }
        }
    }

    public void x2Thepoints()
    {       
        Totalpointts += pointts;
        PlayerPrefs.SetInt("points", Totalpointts);
        PlayerPrefs.Save();
        saveit = true;

        BtnX2.interactable = false;
        congratulationspop.SetActive(true);
        TotalPointsText.text = "Total Points : " + Totalpointts;
    }
}

Upvotes: 0

Views: 963

Answers (2)

Ben
Ben

Reputation: 4110

Unity Google Ads callback events are not guaranteed to be called in the main Unity thread. Try to set you score this way:

public void HandleOnRewarded(object sender, EventArgs args) // User Finished Wacthing The Ad
{ 
    MobileAdsEventExecutor.ExecuteInUpdate(() =>
    {
        // Funtion here to double the coins
        x2Thepoints();
    });
}

The method MobileAdsEventExecutor.ExecuteInUpdate runs the callback in the main Unity thread (unity is mono thread).

Upvotes: -1

Lexicon
Lexicon

Reputation: 483

  1. Set the ad related event handlers before adReward.LoadAd(request, idReward);

    adReward.OnAdLoaded += this.HandleOnRewardedAdLoaded;
    adReward.OnAdRewarded += this.HandleOnRewarded;
    adReward.OnAdClosed += this.HandleOnRewardedAdClosed;
    adReward.LoadAd(request, idReward);
    
  2. Event handlers are not guaranteed to execute in the Unity main thread. So, force execute them in the next update:

public void HandleOnRewarded(object sender, EventArgs args)
{
   MobileAdsEventExecutor.ExecuteInUpdate(() => {
    x2Thepoints();
  });
}

Upvotes: -1

Related Questions