0

So I added a Reward based ad on my Unity Project, I want to reward the user with 300 coins for watching an ad but I'm getting duplicated rewards everytime. I think I know where is the problem, but I can't solve it!

I have this Start Function and a function to get the reward on my Ad Script:

    void Start()
{
    this.rewardBasedVideoAd = RewardBasedVideoAd.Instance;
    rewardBasedVideoAd.OnAdLoaded += HandleOnAdLoaded;
    rewardBasedVideoAd.OnAdOpening += HandleOnAdOpening;
    rewardBasedVideoAd.OnAdClosed += HandleOnAdClosed;
    rewardBasedVideoAd.OnAdRewarded += HandleOnAdRewarded;
    MobileAds.Initialize(initStatus => { });
    this.LoadRewardBasedAd();
}

public void HandleOnAdRewarded(object sender, Reward args)
{
    PlayerPrefs.SetInt("coins", PlayerPrefs.GetInt("coins") + 300);   

}

When you die in the game and want to start again I call SceneManager.LoadScene(0); which is the only Scene I got! I think when I Load Scene the Start function is running again and adding another reward rewardBasedVideoAd.OnAdRewarded += HandleOnAdRewarded;. I've tried calling rewardBasedVideoAd.OnAdRewarded -= HandleOnAdRewarded; several times and in different moments but it didn't solve the problem.

If I restart the game n times in a row, when I watch an ad I will get the Reward n times! Can someone help me? Thank you in advance!

2 Answers2

1

Pretty sure you want to keep only one instance of this GameObject throughout your whole game.

I would call DontDestroyOnLoad for this GameObject.

I think you will need to tweak your Start function, you would need to look if you already have such object in scene before you do your stuff (just tag this GameObject with specific tag and look for objects with that tag in your start function). (Follow example in Unity Documentation I've attached above. DontDestroy.cs is what you need from there)

rCgLT
  • 128
  • 6
  • This probably would work, but I already found a "solution", it's not the best but it worked fine. Instead of calling `PlayerPrefs.SetInt("coins", PlayerPrefs.GetInt("coins") + 300); `Everytime I die in the game I take the number of coins to the "coins" variable and then call `PlayerPrefs.SetInt("coins", coins + 300);`. I know that's not the best way coz it is still doing the reward multiple times, but at least the final result is the same! Thank you – Henrique Sousa May 10 '20 at 11:46
0

Just add OnDestroy function and unsubscribe from events. That's all.