Search Unity

  1. New Unity Live Help updates. Check them out here!

    Dismiss Notice

Unity reward add multiplying reward

Discussion in 'Unity Ads' started by Kobaltic1, Mar 26, 2020.

  1. Kobaltic1

    Kobaltic1

    Joined:
    Jan 22, 2015
    Posts:
    151
    I implemented the Unity reward ad. I have a button at the start of each level. If you watch the ad you get the reward. This works and I get the reward. The issue is it works once before level one. But before level 2 it gives the reward twice. Before level 3 it gives the reward 3 times.

    I have debugged it and it is going through the the OnUnityAdsDidFinish function multiple times. I don't know why.
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3. using UnityEngine.Advertisements;
    4.  
    5. namespace deathToZombies
    6. {
    7.     [RequireComponent(typeof(Button))]
    8.     public class RewardedAdsButton : MonoBehaviour, IUnityAdsListener
    9.     {
    10.  
    11. #if UNITY_IOS
    12.     private string gameId = "1486551";
    13. #elif UNITY_ANDROID
    14.         private string gameId = "1486550";
    15. #endif
    16.  
    17.         Button myButton;
    18.         public string myPlacementId = "rewardedVideo";
    19.         private float levelCheck;
    20.  
    21.         void Start()
    22.         {
    23.             myButton = GetComponent<Button>();
    24.  
    25.             // Set interactivity to be dependent on the Placement’s status:
    26.             myButton.interactable = Advertisement.IsReady(myPlacementId);
    27.  
    28.             // Map the ShowRewardedVideo function to the button’s click listener:
    29.             if (myButton) myButton.onClick.AddListener(ShowRewardedVideo);
    30.  
    31.             // Initialize the Ads listener and service:
    32.             Advertisement.AddListener(this);
    33.             Advertisement.Initialize(gameId, true);
    34.         }
    35.  
    36.         // Implement a function for showing a rewarded video ad:
    37.         void ShowRewardedVideo()
    38.         {
    39.             Advertisement.Show(myPlacementId);
    40.         }
    41.  
    42.         // Implement IUnityAdsListener interface methods:
    43.         public void OnUnityAdsReady(string placementId)
    44.         {
    45.             // If the ready Placement is rewarded, activate the button:
    46.             if (placementId == myPlacementId)
    47.             {
    48.                 myButton.interactable = true;
    49.             }
    50.         }
    51.  
    52.         public void OnUnityAdsDidFinish(string placementId, ShowResult showResult)
    53.         {
    54.             // Define conditional logic for each ad completion status:
    55.             if (showResult == ShowResult.Finished)
    56.             {
    57.                 Debug.Log("I have rewarded me!!!");
    58.                 levelCheck = SpawnManager.level;
    59.                 if (levelCheck % 5 == 0)
    60.                 {
    61.                     SpawnManager.saveToken++;
    62.                 }
    63.                 else
    64.                 {
    65.                     SpawnManager.slowMoToken++;
    66.                 }
    67.             }
    68.             else if (showResult == ShowResult.Skipped)
    69.             {
    70.                 // Do not reward the user for skipping the ad.
    71.             }
    72.             else if (showResult == ShowResult.Failed)
    73.             {
    74.                 Debug.LogWarning("The ad did not finish due to an error.");
    75.             }
    76.         }
    77.  
    78.         public void OnUnityAdsDidError(string message)
    79.         {
    80.             // Log the error.
    81.         }
    82.  
    83.         public void OnUnityAdsDidStart(string placementId)
    84.         {
    85.             // Optional actions to take when the end-users triggers an ad.
    86.         }
    87.     }
    88. }
     
  2. Kobaltic1

    Kobaltic1

    Joined:
    Jan 22, 2015
    Posts:
    151
    Do I need to remove the listener after the reward is given?

    EDIT: I removed the listener and it seemed to work. However I am not sure if this the correct way to handle this issue.
     
  3. ap-unity

    ap-unity

    Unity Technologies

    Joined:
    Aug 3, 2016
    Posts:
    1,384
    @Kobaltic1 Can you confirm the version of Unity Ads you are using?
     
  4. Kobaltic1

    Kobaltic1

    Joined:
    Jan 22, 2015
    Posts:
    151
    I am using 3.4.4
    upload_2020-3-27_21-13-12.png

    I don't destroy the button, I set the enabled to false and then to true. Not sure if that would contribute to the problem.
     
unityunity