2

I want a RewardAd in my game. When you watch video you get +10 score to your current score not your high-score.

You have a 45 high-score and you are now at 37, so you watch video for +10 score and you have 47 high-score it's fine. But if you do it again, the video gives you +20 score?! And the following time +30 and so on.

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

public class RewardAd : MonoBehaviour {

    public int highscore;
    public int score;
    public static GameObject drawscore_obj;
    public RewardBasedVideoAd rewardBasedVideo = null;
    public string adUnitId;

    void Start()
    {
        rewardBasedVideo = null;
        GetComponent<SpriteRenderer> ().enabled = false;
        //highscore = PlayerPrefs.GetInt ("highscore");
        adUnitId = "ca-app-pub-2879768424205988/1590886374";
        rewardBasedVideo = RewardBasedVideoAd.Instance;
        AdRequest request = new AdRequest.Builder().Build();
        rewardBasedVideo.LoadAd(request, adUnitId);
        rewardBasedVideo.OnAdRewarded += HandleRewardBasedVideoRewarded;
    }
    void Update()
    {
        if (GameOverScript.GameOver) 
        {
            GetComponent<SpriteRenderer> ().enabled = true;
        }
    }

    void OnMouseDown()
    {
        showAdd(rewardBasedVideo);
    }

    private void showAdd(RewardBasedVideoAd rewardBasedVideo)
    {
        if (rewardBasedVideo.IsLoaded())
        {
            if (GameOverScript.GameOver) 
            {
                rewardBasedVideo.Show ();
            }
        }
    }

    public void HandleRewardBasedVideoRewarded(object sender, Reward args)
    {   
        highscore = PlayerPrefs.GetInt ("highscore");

        if ((Controll.points + 10) > highscore) 
        {
            Controll.points += 10;
            if(Controll.points > highscore)
            {
                PlayerPrefs.SetInt ("highscore", highscore);
            }
        }
    }
}
antal1208
  • 85
  • 1
  • 7
  • Welcome to SO! Your question would be more clear if you could explain whether the method is being called twice or if it's being called once but adding 20 points. Looks like it could be because you add the event each time the ad is viewed, but never remove it. – DSway Oct 25 '17 at 19:08
  • Thanks! :) Sry for my bad english... i want to said the method called twice and add 10+10 points. Cuz i checked method calls with Debug.Log! :) – antal1208 Oct 26 '17 at 13:27

2 Answers2

2

If you look at this method:

private void showAdd(RewardBasedVideoAd rewardBasedVideo)
{
    if (rewardBasedVideo.IsLoaded())
    {
        if (GameOverScript.GameOver) 
        {
            rewardBasedVideo.Show ();
            rewardBasedVideo.OnAdRewarded += HandleRewardBasedVideoRewarded;
        }
    }
}

Every time it's called, it will add another listener of HandleRewardBasedVideoRewarded to OnAdRewarded. So, the first time it will call HandleRewardBasedVideoRewarded once; the second time twice; and so on.

Normally, you would only add a listener once:

void Start()
{
    rewardBasedVideo = null;
    GetComponent<SpriteRenderer> ().enabled = false;
    //highscore = PlayerPrefs.GetInt ("highscore");
    adUnitId = "ca-app-pub-2**97684242*****/15*08*****";
    rewardBasedVideo = RewardBasedVideoAd.Instance;
    AdRequest request = new AdRequest.Builder().Build();
    rewardBasedVideo.LoadAd(request, adUnitId);

    // Add a listener only when the script is loaded
    rewardBasedVideo.OnAdRewarded += HandleRewardBasedVideoRewarded;
}

private void showAdd(RewardBasedVideoAd rewardBasedVideo)
{
    if (rewardBasedVideo.IsLoaded())
    {
        if (GameOverScript.GameOver) 
        {
            rewardBasedVideo.Show ();
        }
    }
}
Camilo Terevinto
  • 31,141
  • 6
  • 88
  • 120
0

I solved my problem! I think its little bit noob bits its work... :D

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

    public class RewardAd : MonoBehaviour {

        public bool called;
        public int highscore;
        public int score;
        public static GameObject drawscore_obj;
        public RewardBasedVideoAd rewardBasedVideo = null;
        public string adUnitId;

        void Start()
        {
            called = false;
            rewardBasedVideo = null;
            GetComponent<SpriteRenderer> ().enabled = false;
            //highscore = PlayerPrefs.GetInt ("highscore");
            adUnitId = "ca-app-pub-2879768424205988/1590886374";
            rewardBasedVideo = RewardBasedVideoAd.Instance;
            AdRequest request = new AdRequest.Builder().Build();
            rewardBasedVideo.LoadAd(request, adUnitId);
            rewardBasedVideo.OnAdRewarded += HandleRewardBasedVideoRewarded;
        }
        void Update()
        {
            if (GameOverScript.GameOver) 
            {
                GetComponent<SpriteRenderer> ().enabled = true;
            }
        }

        void OnMouseDown()
        {
            showAdd(rewardBasedVideo);
        }

        private void showAdd(RewardBasedVideoAd rewardBasedVideo)
        {
            if (rewardBasedVideo.IsLoaded())
            {
                if (GameOverScript.GameOver) 
                {
                    rewardBasedVideo.Show ();
                    called = true;
                }
            }
        }

        public void HandleRewardBasedVideoRewarded(object sender, Reward args)
        {   
            if (called == true) 
            {
                called = false;
                highscore = PlayerPrefs.GetInt ("highscore");

                if ((Controll.points + 10) > highscore) {
                    Controll.points += 10;
                    if (Controll.points > highscore) {
                        PlayerPrefs.SetInt ("highscore", highscore);
                    }
                }
            }
         }
      }
antal1208
  • 85
  • 1
  • 7