Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Bug My Ads show properly on the Editor, but not on my Android build

Discussion in 'Editor & General Support' started by MBeckmann, Nov 15, 2022.

  1. MBeckmann

    MBeckmann

    Joined:
    Feb 20, 2018
    Posts:
    1
    As the title says, my ads run correctly on the editor. Still, when I build it for Android, even though there is not a single error on the console, the ads don't run at all, but also the OnUnityAdsShowFailure function isn't triggered, since I made it so that when that function is called, the player can keep playing anyways, but this doesn't happen.

    I also checked that the SDK is initialized and finishes initializing before the Ad is loaded, so I don't know what I can do, I have tried every single response I found online, but nothing seems to work.

    I am using Unity 2021.3.13f1 Personal, and this is how my AdsInitializer and InterstitialAdsButton scripts look like:

    AdsInitializer:

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.Advertisements;
    3. public class AdsInitializer : MonoBehaviour, IUnityAdsInitializationListener
    4. {
    5.     [SerializeField] string _androidGameId;
    6.     [SerializeField] string _iOSGameId;
    7.     [SerializeField] bool _testMode = true;
    8.     private string _gameId = "5013507";
    9.  
    10.     [SerializeField] InterstitialAdsButton interstitialAdsButton;
    11.  
    12.     void Awake()
    13.     {
    14.         InitializeAds();
    15.     }
    16.  
    17.  
    18.     public void InitializeAds()
    19.     {
    20.         Advertisement.Initialize(_gameId, _testMode, this);
    21.     }
    22.     public void OnInitializationComplete()
    23.     {
    24.         Debug.Log("Unity Ads initialization complete.");
    25.         interstitialAdsButton.LoadAd();
    26.     }
    27.     public void OnInitializationFailed(UnityAdsInitializationError error, string message)
    28.     {
    29.         Debug.Log($"Unity Ads Initialization Failed: {error.ToString()} - {message}");
    30.     }
    31. }
    InterstitialAdsButton:

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.Advertisements;
    3.  
    4. public class InterstitialAdsButton : MonoBehaviour, IUnityAdsLoadListener, IUnityAdsShowListener
    5. {
    6.     [SerializeField] string _androidAdUnitId = "Interstitial_Android";
    7.     [SerializeField] string _iOsAdUnitId = "Interstitial_iOS";
    8.     string _adUnitId;
    9.     static int playNumber = 0;
    10.  
    11.     [SerializeField] AdsInitializer adsInitializer;
    12.  
    13.     void Awake()
    14.     {
    15.         // Get the Ad Unit ID for the current platform:
    16.         _adUnitId = (Application.platform == RuntimePlatform.IPhonePlayer)
    17.             ? _iOsAdUnitId
    18.             : _androidAdUnitId;
    19.     }
    20.  
    21.     // Load content to the Ad Unit:
    22.     public void LoadAd()
    23.     {
    24.         // IMPORTANT! Only load content AFTER initialization (in this example, initialization is handled in a different script).
    25.         Debug.Log("Loading Ad: " + _adUnitId);
    26.         Advertisement.Load(_adUnitId, this);
    27.     }
    28.  
    29.     public void ClickRestart()
    30.     {
    31.         if (playNumber < 4)
    32.         {
    33.             playNumber = playNumber + 1;
    34.             Debug.Log("Play number " + playNumber);
    35.             PlayerPrefs.SetInt("restart", 1);
    36.         }
    37.  
    38.         if (playNumber == 4)
    39.         {
    40.             // Note that if the ad content wasn't previously loaded, this method will fail
    41.             Debug.Log("Showing Ad: " + _adUnitId);
    42.             ShowAd();
    43.             PlayerPrefs.SetInt("restart", 0);
    44.             Debug.Log("Play number" + playNumber);
    45.         }
    46.  
    47.         if (playNumber == 5)
    48.         {
    49.             playNumber = 0;
    50.         }
    51.     }
    52.  
    53.     // Show the loaded content in the Ad Unit:
    54.     public void ShowAd()
    55.     {
    56.         Advertisement.Show(_adUnitId, this);
    57.     }
    58.  
    59.     // Implement Load Listener and Show Listener interface methods:
    60.     public void OnUnityAdsAdLoaded(string adUnitId)
    61.     {
    62.         // Optionally execute code if the Ad Unit successfully loads content.
    63.     }
    64.  
    65.     public void OnUnityAdsFailedToLoad(string adUnitId, UnityAdsLoadError error, string message)
    66.     {
    67.         Debug.Log($"Error loading Ad Unit: {adUnitId} - {error.ToString()} - {message}");
    68.         // Optionally execute code if the Ad Unit fails to load, such as attempting to try again.
    69.     }
    70.  
    71.     public void OnUnityAdsShowFailure(string adUnitId, UnityAdsShowError error, string message)
    72.     {
    73.         Debug.Log($"Error showing Ad Unit {adUnitId}: {error.ToString()} - {message}");
    74.         // Optionally execute code if the Ad Unit fails to show, such as loading another ad.
    75.         playNumber = 0;
    76.     }
    77.  
    78.     public void OnUnityAdsShowStart(string adUnitId) { }
    79.     public void OnUnityAdsShowClick(string adUnitId) { }
    80.     public void OnUnityAdsShowComplete(string adUnitId, UnityAdsShowCompletionState showCompletionState)
    81.     {
    82.         playNumber = 0;
    83.     }
    84. }
    And this is what my Package Manager looks like (I did update these two packages that aren't up to date, in a clone of this project that I am using to try different solutions, but didn't make any difference):

    screenshot.PNG
     
  2. ShinyOpal

    ShinyOpal

    Joined:
    Dec 17, 2022
    Posts:
    32
    Is there a particular reason for using the Advertisement Legacy 4.4.1 ?
    Why not using Advertisement 4.3.0 ?