Search Unity

Bug Rewarded Ads keep doubling in Editor, works fine on Phone

Discussion in 'Unity Ads & User Acquisition' started by N1ghtrunner, Mar 19, 2023.

  1. N1ghtrunner

    N1ghtrunner

    Joined:
    Jan 5, 2015
    Posts:
    104
    I have a shop that has a Rewarded ad button that allows you to 'top up' your gold coins by watching a rewarded ad. The code is per the documentation and for brevity and the script that contains the code is attached to the button (i.e. the button doesnt use on OnClick event), below is what is running when when the ad completes:

    Code (CSharp):
    1.     public void OnUnityAdsShowComplete(string adUnitId, UnityAdsShowCompletionState showCompletionState)
    2.     {
    3.         if (adUnitId.Equals(_adUnitId) && showCompletionState.Equals(UnityAdsShowCompletionState.COMPLETED))
    4.         {
    5.             ReportGoldTopUp(topupGoldVideo);
    6.             Debug.Log("Unity Ads Rewarded Ad Completed");
    7.             FindObjectOfType<ShopManager>().RewardedAdTopUp();
    8.             // Load another ad:
    9.             Advertisement.Load(_adUnitId, this);
    10.         }
    11.     }
    ReportGoldTopUp is an analytics event to capture the event.
    The RewardedAdTopUp() method looks like this (very simple stuff):

    Code (CSharp):
    1. public void RewardedAdTopUp()
    2. {
    3.     gameStatus.currentGold = gameStatus.currentGold + 200;
    4.     PlayerPrefs.SetInt("CurrentGold", gameStatus.currentGold);
    5.     shopGoldUI.text = PlayerPrefs.GetInt("CurrentGold").ToString();
    6.     Debug.Log("Gold should be " + gameStatus.currentGold);
    7. }
    What is happening in the Unity Editor:
    • I can click on the rewarded ads button multiple times as expected
    • The first time I click on it, everything runs as expected - the gold value which is currently stored as a PlayerPref is updated with an additional 200 gold coins - I see my Debug.log show up in the console with the correct gold value expected.
    • The second time I click on the button, the method appears to run TWICE as I see the Deblog.log event come up twice in the console, iterating the gold by 200 each time. So basically the first click is rewarding 200 (correctly), the second click is rewarding 400 (incorrectly), the third click will reward 800 etc etc.
    I cant see an obvious reason for why this would be, or if I am missing some kind of control to prevent this from happening. All of this said though, when testing this out on my actual Android device, with ads currently in test-mode, the increments work correctly and there are no issues - i.e. I can click it multiple times and the reward is 200 each time.

    Why is this happening and why is there a discrepency between the editor and a device in this regard??
     
  2. SamOYUnity3D

    SamOYUnity3D

    Unity Technologies

    Joined:
    May 12, 2019
    Posts:
    626
    You may have registered the listener multiple times so that the callback is called multiple times, please check. If you are sure this is not the cause, please contact us and provide a demo project that can reproduce the issue.
    https://docs.unity.com/ads/en/manual/Support
     
  3. N1ghtrunner

    N1ghtrunner

    Joined:
    Jan 5, 2015
    Posts:
    104
    Ok interesting I didn't realise this could be the cause of this issue. I have a game object that contains a script for Interstitial Ads attached to my Game Status/Game Manager singleton. I need to have this present in every scene in order for the various ads to run. i.e. Rewarded Ads buttons are disabled unless this is in the scene?

    Code (CSharp):
    1. public class ADS_InterstitialAds : MonoBehaviour, IUnityAdsLoadListener, IUnityAdsShowListener
    So i guess this listener is active when I click on the shop rewarded ad button and thats causing the problem??

    Code (CSharp):
    1. public class ADS_RewardedAds_GoldTopUp : MonoBehaviour, IUnityAdsLoadListener, IUnityAdsShowListener
    Whats the best way to tackle this? Remove the listener from the rewarded ads scripts?
     
  4. SamOYUnity3D

    SamOYUnity3D

    Unity Technologies

    Joined:
    May 12, 2019
    Posts:
    626
    You need to make sure there is only one listener in the game, you can use the singleton pattern, and only use one script to control the advertisement. Here are some examples, please let me know if you have any questions. https://gamedev.stackexchange.com/q...o-i-correctly-implement-the-singleton-pattern
     
    N1ghtrunner likes this.