Search Unity

Problem with Callback OnUnityAdsDidFinish

Discussion in 'Unity Ads & User Acquisition' started by ZeqDEV, Oct 22, 2019.

  1. ZeqDEV

    ZeqDEV

    Joined:
    Apr 15, 2018
    Posts:
    1
    I have different kind of scripts such as:

    Interstacial ads
    Rewarded ads

    The problem is that whenever e.g. I'm watching an interstacial ad I get the reward from all "active" scripts with OnUnityAdsDidFinish in it. Which runs all my rewards from ALL the scripts with a OnUnityAdsDidFinish in it, even though I have separate placementIds in the different scripts.

    Code (CSharp):
    1. public void OnUnityAdsDidFinish(string placementId, ShowResult showResult) {
    2.  
    3.         if (showResult == ShowResult.Finished)
    4.         {
    5.             MoneyManager.Instance.AddMoney(20);
    6.             Debug.Log("AD ROLL TWO COMPLETE");
    7.         }
    8.         else if (showResult == ShowResult.Skipped)
    9.         {
    10.  
    11.         }
    12.         else if (showResult == ShowResult.Failed)
    13.         {
    14.  
    15.         }
    16.     }
    If I have two separate scripts with different placementIDs with addmoney(20) I will get 40 in value instead of 20 as I want.

    Am I missing something?

    Thanks in advance

    I'm running 2019.2.0f1
     
  2. jacobhedengran

    jacobhedengran

    Joined:
    Jul 21, 2019
    Posts:
    1
    Hey, did you solve it because I'm having the same issue now.
     
  3. sbankhead

    sbankhead

    Unity Technologies

    Joined:
    Jul 27, 2014
    Posts:
    97
    You need to be checking the placementId in your OnUnityAdsDidFinish callback. Every listener is called when an ad becomes ready, starts, finishes, etc. and the placementId is sent so that you can determine which placement Id the callback is referring to.
     
  4. Pixel8GameStudio

    Pixel8GameStudio

    Joined:
    Jul 15, 2020
    Posts:
    6
    Don't forget to attach the ads listner to your class, where you initialized the ad object.

    Something like that:
    Code (CSharp):
    1.     private void Start() {
    2.         Advertisement.Initialize(google_id);
    3.         Advertisement.AddListener(this);
    4.     }
     
  5. cynuro

    cynuro

    Joined:
    Sep 18, 2014
    Posts:
    8
    For everyone else having the same issue, what I did is to make it so you add Listener when you press the button and remove it when the ad is succesful. This way you avoid having all scripts waiting for an Ad to pop.
    Idk if its the best way but it works so :p
     
    zainabikram7878 and kyle-unity like this.
  6. Pixel8GameStudio

    Pixel8GameStudio

    Joined:
    Jul 15, 2020
    Posts:
    6
    I would not recommand adding listeners on the fly, especially on user interaction. If the user spams the button, it may stack your listener several times and this can actually slow your interface because all the listeners copies will be triggered in the same time.
    The scripts that dont need the listener will not wait for the ad to pop to do their job, in fact, listeners are kind of asynchronous tasks.
     
  7. knaukhaiz1

    knaukhaiz1

    Joined:
    Jul 28, 2020
    Posts:
    1
    Or you can put a simple check to only add the listener if there's no listener component attached currently :)
     
  8. Pixel8GameStudio

    Pixel8GameStudio

    Joined:
    Jul 15, 2020
    Posts:
    6
    It's more resource consuming this way, your check will be running all the time.
     
  9. ArcaDone

    ArcaDone

    Joined:
    Jan 6, 2021
    Posts:
    10
    Could Anyone suggest the better way to solve it? I've the same issue
     
  10. zainabikram7878

    zainabikram7878

    Joined:
    Nov 6, 2020
    Posts:
    1
    Beautt
     
  11. MishDotCom

    MishDotCom

    Joined:
    Jan 17, 2021
    Posts:
    34
    This worked for me! Thank you!
     
  12. Gothrond

    Gothrond

    Joined:
    Jul 2, 2014
    Posts:
    6
    Thank you for the tip to attach the ads listener, exactly fixed the problem I was having