Search Unity

Banner ad when clicked opens page multiple times

Discussion in 'Unity Ads & User Acquisition' started by Ramonjr, May 16, 2020.

  1. Ramonjr

    Ramonjr

    Joined:
    Apr 4, 2020
    Posts:
    7
    Hi everyone

    i hope you're all doing well during this strange time

    i've only just implemented ads on my game and I am having two problems

    when clicking on the banner add, it opens a random amount of web pages, sometimes it opens 1 and other times 10 - 20!

    my InterstitialAD plays the moment i activate it pressing a button (which is great!) but as soon as the scene starts (retry button, which then reloads the scene) the ad shows again!

    I have two objects
    1 with video ads script attached and another with the banner ads script

    here are my scripts:
    -----------------------------------------------------------------------------------------------------------------------------
    Banner
    -----------------------------------------------------------------------------------------------------------------------------
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.Advertisements;

    public class BannerAds : MonoBehaviour
    {
    string gameId = "xxxxx";
    string myPlacementId = "banner";
    bool testMode = true;

    void Start()
    {
    Advertisement.Initialize(gameId, testMode);
    StartCoroutine(ShowBannerWhenReady());
    }

    IEnumerator ShowBannerWhenReady()
    {
    while (!Advertisement.IsReady(myPlacementId))
    {
    yield return new WaitForSeconds(0.5f);

    }
    Advertisement.Banner.SetPosition(UnityEngine.Advertisements.BannerPosition.BOTTOM_CENTER);
    Advertisement.Banner.Show(myPlacementId);
    }
    }

    -----------------------------------------------------------------------------------------------------------------------------
    Video Ads
    -----------------------------------------------------------------------------------------------------------------------------

    using UnityEngine;
    using UnityEngine.Advertisements;

    public class UnityMonetization : MonoBehaviour, IUnityAdsListener
    {


    string gameId = "xxxxx";
    string myPlacementId = "rewardedVideo";
    bool testMode = true;

    void Start()
    {
    Advertisement.AddListener(this);
    // Initialize the Ads service:
    Advertisement.Initialize(gameId, testMode);
    // Show an ad:
    //Advertisement.Show();
    }

    public void DisplayInterstitialAD()
    {
    Advertisement.Show();
    }

    public void DisplayVideoAD()
    {
    Advertisement.Show(myPlacementId);
    }

    // Implement IUnityAdsListener interface methods:
    public void OnUnityAdsDidFinish(string placementId, ShowResult showResult)
    {
    // Define conditional logic for each ad completion status:
    if (showResult == ShowResult.Finished)
    {
    // Reward the user for watching the ad to completion.
    Debug.LogWarning("You receive reward for watching the video");

    }
    else if (showResult == ShowResult.Skipped)
    {
    // Do not reward the user for skipping the ad.
    Debug.LogWarning("You don't receive reward for skipping the video");
    }
    else if (showResult == ShowResult.Failed)
    {
    Debug.LogWarning("The ad did not finish due to an error.");
    }
    }

    public void OnUnityAdsReady(string placementId)
    {
    // If the ready Placement is rewarded, show the ad:
    if (placementId == myPlacementId)
    {

    }
    }

    public void OnUnityAdsDidError(string message)
    {
    // Log the error.
    }

    public void OnUnityAdsDidStart(string placementId)
    {
    // Optional actions to take when the end-users triggers an ad.
    }

    }

    ------------------------------------------------------------------
    END
    ------------------------------------------------------------------

    Thanks for your time!
     
  2. kyle-unity

    kyle-unity

    Unity Technologies

    Joined:
    Jan 6, 2020
    Posts:
    336
    Can you try adding the following code to your ads classes to see if this fixes it:
    Code (CSharp):
    1. private void OnDestroy()
    2. {
    3.     Advertisement.RemoveListener(this);
    4. }
    When you call
    Advertisement.AddListener(this);
    , this causes your ads code to persist once the scene changes, which can cause all sorts of unusual bugs when there are mutliple versions of your ads classes all running at the same time. The code snippet above will let your ads code be properly removed once the scene changes.
     
    Ramonjr likes this.