Search Unity

Unity Ad showing multiple times

Discussion in 'Unity Ads & User Acquisition' started by trebuH, Feb 13, 2020.

Thread Status:
Not open for further replies.
  1. trebuH

    trebuH

    Joined:
    Nov 21, 2018
    Posts:
    3
    Hi! I have a problem with Unity Ads. I have a button that starts an ad when clicked on. It seems that the ad is getting started multiple times even though the button registers one click. This leads to the player being rewarded multiple times. Does anyone know what might be causing this issue?

    The part of the code I'm using:
    Code (CSharp):
    1.  public void Click()
    2.         {
    3.             FindObjectOfType<AudioManager>().Play("Button");
    4.             PlayerPrefs.SetInt("RunsTillCanWatchAd", 3);
    5.             Advertisement.Show("rewardedVideo");
    6.             Debug.Log("Click() called");
    7.         }
    8.  
    9.         public void OnUnityAdsDidFinish(string placementId, ShowResult showResult)
    10.         {
    11.             Debug.Log("Ad Finished");
    12.             if(showResult == ShowResult.Finished)
    13.             {
    14.                 Debug.Log("Added revive");
    15.                 PlayerPrefs.SetInt("Revive", PlayerPrefs.GetInt("Revive") + 1);
    16.             }
    17.             else if(showResult == ShowResult.Skipped)
    18.             {
    19.                 Debug.Log("The ad did not finish; skipped");
    20.             }
    21.             else if(showResult == ShowResult.Failed)
    22.             {
    23.                 Debug.LogWarning("The ad did not finish due to an error.");
    24.             }
    25.         }
    And that's the log I'm getting:

    Thanks for the answers!
     
  2. sbankhead

    sbankhead

    Unity Technologies

    Joined:
    Jul 27, 2014
    Posts:
    97
    The code you showed looked fine and is likely not the problem. It likely has something to do with a a listener being added twice. Possibly your adding one in a start method and you have that script on more than one object, or your enabling and disabling that object causing start to get called more than once. hard to say without seeing more code, but this is where I would focus my debugging at. find all instances of AddListener, and then find all usages of those scripts and see if that helps you narrow down the issue.
     
    Jainnikita279, Simonotos and trebuH like this.
  3. trebuH

    trebuH

    Joined:
    Nov 21, 2018
    Posts:
    3
    Thank you for the quick reply!
    This script is present only on the one object, the button itself. I am changing the button's intractable value though. Here's the whole script if that helps:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5. using UnityEngine.Advertisements;
    6.  
    7. public class ReviveHandler : MonoBehaviour, IUnityAdsListener
    8. {
    9.     #region Variebles
    10.     public Sprite redHeart;
    11.     public Sprite whiteHeart;
    12.     public Button bt;
    13.     #endregion
    14.  
    15.     void Start()
    16.     {
    17.         Advertisement.AddListener(this);
    18.         Advertisement.Initialize("3435969", true);
    19.  
    20.         if(!PlayerPrefs.HasKey("Revive"))
    21.         {
    22.             PlayerPrefs.SetInt("Revive", 3);
    23.         }
    24.  
    25.         if(!PlayerPrefs.HasKey("RunsTillCanWatchAd"))
    26.         {
    27.             PlayerPrefs.SetInt("RunsTillCanWatchAd", 0);
    28.         }
    29.     }
    30.  
    31.  
    32.     void Update()
    33.     {
    34.         #region DEBUG
    35.  
    36.         if(Input.GetKeyDown(KeyCode.W))
    37.         {
    38.             PlayerPrefs.SetInt("Revive", PlayerPrefs.GetInt("Revive") + 1);
    39.         }
    40.  
    41.         if(Input.GetKeyDown(KeyCode.Q))
    42.         {
    43.             PlayerPrefs.SetInt("Revive", PlayerPrefs.GetInt("Revive") - 1);
    44.         }
    45.         #endregion
    46.  
    47.         if((PlayerPrefs.GetInt("RunsTillCanWatchAd") <= 0) && PlayerPrefs.GetInt("Revive") < 5 && Advertisement.IsReady("rewardedVideo"))
    48.         {
    49.             SetSprite(redHeart);
    50.             bt.interactable = true;
    51.         }
    52.         else if(PlayerPrefs.GetInt("Revive") >= 5 || PlayerPrefs.GetInt("RunsTillCanWatchAd") >= 0 || !Advertisement.IsReady())
    53.         {
    54.             SetSprite(whiteHeart);
    55.             bt.interactable = false;
    56.         }
    57.      
    58.     }
    59.  
    60.     private void SetSprite(Sprite toSet)
    61.     {
    62.         gameObject.GetComponent<Image>().sprite = toSet;
    63.     }
    64.  
    65.     public void Click()
    66.     {
    67.         FindObjectOfType<AudioManager>().Play("Button");
    68.         PlayerPrefs.SetInt("RunsTillCanWatchAd", 3);
    69.         Advertisement.Show("rewardedVideo");
    70.         Debug.Log("Click() called");
    71.     }
    72.  
    73.     public void OnUnityAdsDidFinish(string placementId, ShowResult showResult)
    74.     {
    75.         Debug.Log("Ad Finished");
    76.         if(showResult == ShowResult.Finished)
    77.         {
    78.             Debug.Log("Added revive");
    79.             PlayerPrefs.SetInt("Revive", PlayerPrefs.GetInt("Revive") + 1);
    80.         }
    81.         else if(showResult == ShowResult.Skipped)
    82.         {
    83.             Debug.Log("The ad did not finish; skipped");
    84.         }
    85.         else if(showResult == ShowResult.Failed)
    86.         {
    87.             Debug.LogWarning("The ad did not finish due to an error.");
    88.         }
    89.     }
    90.     public void OnUnityAdsReady( string placementId)
    91.     {
    92.  
    93.     }
    94.  
    95.     public void OnUnityAdsDidError(string placementId)
    96.     {
    97.         Debug.LogError("Unity Ads error ocured!");
    98.     }
    99.  
    100.     public void OnUnityAdsDidStart(string placementId)
    101.     {
    102.         Debug.Log("Ad started");
    103.     }
    104. }
    105.  
     
    Zlodew likes this.
  4. sbankhead

    sbankhead

    Unity Technologies

    Joined:
    Jul 27, 2014
    Posts:
    97
    So I would put a debug.log statement right above AddListener to see if this script is getting called more than once. If it is, fix that. You could also try adding a static bool to gate the addListener and init logic to prevent it from happening more than once.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5. using UnityEngine.Advertisements;
    6. public class ReviveHandler : MonoBehaviour, IUnityAdsListener
    7. {
    8.     #region Variebles
    9.     public Sprite redHeart;
    10.     public Sprite whiteHeart;
    11.     public Button bt;
    12.     #endregion
    13.  
    14.     private static bool isInitialized = false;
    15.     void Start()
    16.     {
    17.         if (!isInitialized) {
    18.             isInitialized = true;
    19.             Advertisement.AddListener(this);
    20.             Advertisement.Initialize("3435969", true);
    21.         }
    22.         if(!PlayerPrefs.HasKey("Revive"))
    23.         {
    24.             PlayerPrefs.SetInt("Revive", 3);
    25.         }
    26.         if(!PlayerPrefs.HasKey("RunsTillCanWatchAd"))
    27.         {
    28.             PlayerPrefs.SetInt("RunsTillCanWatchAd", 0);
    29.         }
    30.     }
    31.     void Update()
    32.     {
    33.         #region DEBUG
    34.         if(Input.GetKeyDown(KeyCode.W))
    35.         {
    36.             PlayerPrefs.SetInt("Revive", PlayerPrefs.GetInt("Revive") + 1);
    37.         }
    38.         if(Input.GetKeyDown(KeyCode.Q))
    39.         {
    40.             PlayerPrefs.SetInt("Revive", PlayerPrefs.GetInt("Revive") - 1);
    41.         }
    42.         #endregion
    43.         if((PlayerPrefs.GetInt("RunsTillCanWatchAd") <= 0) && PlayerPrefs.GetInt("Revive") < 5 && Advertisement.IsReady("rewardedVideo"))
    44.         {
    45.             SetSprite(redHeart);
    46.             bt.interactable = true;
    47.         }
    48.         else if(PlayerPrefs.GetInt("Revive") >= 5 || PlayerPrefs.GetInt("RunsTillCanWatchAd") >= 0 || !Advertisement.IsReady())
    49.         {
    50.             SetSprite(whiteHeart);
    51.             bt.interactable = false;
    52.         }
    53.    
    54.     }
    55.     private void SetSprite(Sprite toSet)
    56.     {
    57.         gameObject.GetComponent<Image>().sprite = toSet;
    58.     }
    59.     public void Click()
    60.     {
    61.         FindObjectOfType<AudioManager>().Play("Button");
    62.         PlayerPrefs.SetInt("RunsTillCanWatchAd", 3);
    63.         Advertisement.Show("rewardedVideo");
    64.         Debug.Log("Click() called");
    65.     }
    66.     public void OnUnityAdsDidFinish(string placementId, ShowResult showResult)
    67.     {
    68.         Debug.Log("Ad Finished");
    69.         if(showResult == ShowResult.Finished)
    70.         {
    71.             Debug.Log("Added revive");
    72.             PlayerPrefs.SetInt("Revive", PlayerPrefs.GetInt("Revive") + 1);
    73.         }
    74.         else if(showResult == ShowResult.Skipped)
    75.         {
    76.             Debug.Log("The ad did not finish; skipped");
    77.         }
    78.         else if(showResult == ShowResult.Failed)
    79.         {
    80.             Debug.LogWarning("The ad did not finish due to an error.");
    81.         }
    82.     }
    83.     public void OnUnityAdsReady( string placementId)
    84.     {
    85.     }
    86.     public void OnUnityAdsDidError(string placementId)
    87.     {
    88.         Debug.LogError("Unity Ads error ocured!");
    89.     }
    90.     public void OnUnityAdsDidStart(string placementId)
    91.     {
    92.         Debug.Log("Ad started");
    93.     }
    94. }
     
  5. trebuH

    trebuH

    Joined:
    Nov 21, 2018
    Posts:
    3
    This bool gate is just what I needed! Thank you for help. Everything is working fine.
     
    kyle-unity likes this.
  6. ALeonidou

    ALeonidou

    Joined:
    Apr 2, 2020
    Posts:
    7
    It is better practise to use this instead of patching the code with a bool gate.

    public void OnDestroy ()
    {
    Advertisement.RemoveListener (this);
    }

    Add this to method to your script and the OnUnityAdsDidFinish() will only be called once. (The reason it was called multiple times was because, although the scene changed and a new "IUnityAdsListener" was created, the old "IUnityAdsListener" from the previous scene was still active, resulting to multiple calls of the OnUnityAdsDidFinish() method. An easy way to prove this is to notice that the number of calls of the method is directly proportional with the number of times you changed the scene!)
     
    Zlodew and Jainnikita279 like this.
Thread Status:
Not open for further replies.