Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Bug Ads not showing

Discussion in 'Unity Ads & User Acquisition' started by hearnodarkness, Oct 6, 2022.

  1. hearnodarkness

    hearnodarkness

    Joined:
    Nov 12, 2020
    Posts:
    4
    I am in need of help because of ads.

    This is my game: Too Deep To Dig – Apps no Google Play

    The problem is: it does not shows ads.

    I am using Advertisement 4.30 in Unity 2020.3.38f1, the ads should show in the beginning. The gameobject for the ads is summoned as a persistent object and start as soon the game starts.

    In editor, it shows perfectly that everything is tuned, but not on the game.

    Also, the test mode is FALSE.

    Here is the code of the ad game object:

    Code (CSharp):
    1. using System;
    2. using UnityEngine;
    3. #if ENABLE_INPUT_SYSTEM || UNITY_Android
    4. using System.Collections;
    5. using RPG.Control;
    6. using UnityEngine.Advertisements;
    7. public class AdManager : MonoBehaviour, IUnityAdsInitializationListener, IUnityAdsLoadListener, IUnityAdsShowListener
    8. {
    9.     [SerializeField] private bool testMode = false;
    10.     [SerializeField] private float delayInit = 0f;
    11.     private string gameId = "4896638";
    12.     private string video = "Level_Start";
    13.     private bool showAd = true;
    14.     private bool initializationIsComplete=false;
    15.     private void Awake()
    16.     {
    17.         //#if UNITY_IOS
    18.         //gameId = "4896639";
    19. //#endif
    20.         Advertisement.Initialize(gameId, testMode, this);
    21.     }
    22.  
    23.     private void OnEnable()
    24.     {
    25.         LoadAd();
    26.     }
    27.  
    28.     private void Update()
    29.     {
    30.         if (Time.timeSinceLevelLoad > delayInit && showAd)
    31.         {
    32.             ShowAd();
    33.         }
    34.     }
    35.  
    36.     // Load content to the Ad Unit:
    37.     public void LoadAd()
    38.     {
    39.         // IMPORTANT! Only load content AFTER initialization.
    40.         Debug.Log("Loading Ad: " + gameId);
    41.         Advertisement.Load(gameId, this);
    42.     }
    43.     public void ShowAd()
    44.     {
    45.         if(!initializationIsComplete)
    46.         {
    47.             Debug.Log($"Trying to show and ad, but the system has not been initialized");
    48.             return;
    49.         }
    50.         if(Advertisement.isInitialized)
    51.         {
    52.             Advertisement.Show(video, this);
    53.         }
    54.     }
    55.     public void OnInitializationComplete()
    56.     {
    57.         print("Initialization complete.");
    58.         initializationIsComplete = true;
    59.     }
    60.     public void OnInitializationFailed(UnityAdsInitializationError error, string message)
    61.     {
    62.         Debug.Log($"Unity Ads Initialization Failed: {error} - {message}");
    63.     }
    64.     public void OnUnityAdsAdLoaded(string placementId)
    65.     {
    66.         Debug.Log($"Ad Loaded: {placementId}");
    67.     }
    68.     public void OnUnityAdsFailedToLoad(string placementId, UnityAdsLoadError error, string message)
    69.     {
    70.         Debug.Log($"Error loading Ad Unit {placementId}: {error} - {message}");
    71.     }
    72.     public void OnUnityAdsShowFailure(string placementId, UnityAdsShowError error, string message)
    73.     {
    74.         Debug.Log($"Error loading Ad Unit {placementId}: {error} - {message}");
    75.     }
    76.     public void OnUnityAdsShowStart(string placementId)
    77.     {
    78.        
    79.     }
    80.     public void OnUnityAdsShowClick(string placementId)
    81.     {
    82.        
    83.     }
    84.     public void OnUnityAdsShowComplete(string placementId, UnityAdsShowCompletionState showCompletionState)
    85.     {
    86.         switch (showCompletionState)
    87.         {
    88.             case UnityAdsShowCompletionState.COMPLETED:
    89.                 showAd = false;
    90.                 break;
    91.             case UnityAdsShowCompletionState.SKIPPED:
    92.                 showAd = false;
    93.                 break;
    94.             case UnityAdsShowCompletionState.UNKNOWN:
    95.                 Debug.LogWarning("Ad Failed");
    96.                 break;
    97.         }
    98.     }
    99.    
    100. }
    101. #endif
     
  2. Unity_Adamski

    Unity_Adamski

    Unity Technologies

    Joined:
    Jul 20, 2020
    Posts:
    110
    Hi, looking at your code I can see that LoadAd is called in the OnEnable function. This is likely causing your LoadAd call to occur before the SDK has fully initialized. I would advise moving this call to your OnInitializationComplete callback.
    I would also make sure that you are calling to load an ad before every show call. The easiest way o do this is to call Load in the ShowComplete callback.
     
    hearnodarkness likes this.
  3. hearnodarkness

    hearnodarkness

    Joined:
    Nov 12, 2020
    Posts:
    4
    Hey friend, thank you to answer, and sorry to take so long! I tried several manners to fix it in this mean time. But with no success. So, I made an APK and tried to debug adding an Text Mesh Pro Component to show the problem. It takes 10 seconds to answer something. The final message was:

    "Error showing Ad Unit Level_Start: NOT_READY - Placement not ready.

    Here is the new code:

    Code (CSharp):
    1. using System;
    2. using UnityEngine;
    3. using System.Collections;
    4. using RPG.Control;
    5. using TMPro;
    6. using UnityEngine.Advertisements;
    7. public class AdManager : MonoBehaviour, IUnityAdsInitializationListener, IUnityAdsLoadListener, IUnityAdsShowListener
    8. {
    9.     [SerializeField] private bool testMode = false;
    10.     [SerializeField] private float delayInit = 5f;
    11.     private string gameId = "4896638";
    12.     private string video = "Level_Start";
    13.     private bool showAd = true;
    14.     private bool initializationIsComplete=false;
    15.     private bool updateInitialization = true;
    16.  
    17.     [SerializeField] private TextMeshProUGUI debugLog;
    18.     private int countDebugLog = 0;
    19.     private void Update()
    20.     {
    21.         if (Time.timeSinceLevelLoad > delayInit && updateInitialization)
    22.         {
    23.            
    24.             //#if UNITY_IOS
    25.             //gameId = "4896639";
    26. //#endif
    27.             Advertisement.Initialize(gameId, testMode, this);
    28.             updateInitialization = false;
    29.             return;
    30.         }  
    31.        
    32.         if (Time.timeSinceLevelLoad > delayInit && showAd)
    33.         {
    34.             ShowAd();
    35.         }
    36.     }
    37.  
    38.     // Load content to the Ad Unit:
    39.     public void LoadAd()
    40.     {
    41.         if (showAd==false)
    42.         {
    43.             return;
    44.         }
    45.         // IMPORTANT! Only load content AFTER initialization.
    46.  
    47.         string debugText = "Loading Ad: " + gameId;
    48.        
    49.         Debug.Log(debugText);
    50.        
    51.         ShowDebug(debugText);
    52.        
    53.         Advertisement.Load(gameId, this);
    54.     }
    55.     public void ShowAd()
    56.     {
    57.         if(!initializationIsComplete)
    58.         {
    59.             //string debugText = $"Trying to show and ad, but the system has not been initialized";
    60.             Debug.Log($"Trying to show and ad, but the system has not been initialized");
    61.             //ShowDebug(debugText);
    62.             return;
    63.         }
    64.         if(Advertisement.isInitialized)
    65.         {
    66.             showAd = false;
    67.             LoadAd();
    68.             Advertisement.Show(video, this);
    69.         }
    70.     }
    71.     public void OnInitializationComplete()
    72.     {
    73.         print("Initialization complete.");
    74.         delayInit = delayInit + Time.timeSinceLevelLoad;
    75.         initializationIsComplete = true;
    76.     }
    77.     public void OnInitializationFailed(UnityAdsInitializationError error, string message)
    78.     {
    79.         string debugText = $"Unity Ads Initialization Failed: {error} - {message}";
    80.         Debug.Log($"Unity Ads Initialization Failed: {error} - {message}");
    81.         ShowDebug(debugText);
    82.         updateInitialization = true;
    83.         showAd = true;
    84.     }
    85.     public void OnUnityAdsAdLoaded(string placementId)
    86.     {
    87.         Debug.Log($"Ad Loaded: {placementId}");
    88.     }
    89.     public void OnUnityAdsFailedToLoad(string placementId, UnityAdsLoadError error, string message)
    90.     {
    91.         string debugText = $"Error loading Ad Unit {placementId}: {error} - {message}";
    92.         Debug.Log($"Error loading Ad Unit {placementId}: {error} - {message}");
    93.         ShowDebug(debugText);
    94.         showAd = true;
    95.     }
    96.     public void OnUnityAdsShowFailure(string placementId, UnityAdsShowError error, string message)
    97.     {
    98.         string debugText = $"Error showing Ad Unit {placementId}: {error} - {message}";
    99.         Debug.Log($"Error showing Ad Unit {placementId}: {error} - {message}");
    100.         ShowDebug(debugText);
    101.         showAd = true;
    102.     }
    103.     public void OnUnityAdsShowStart(string placementId)
    104.     {
    105.        
    106.     }
    107.     public void OnUnityAdsShowClick(string placementId)
    108.     {
    109.        
    110.     }
    111.     public void OnUnityAdsShowComplete(string placementId, UnityAdsShowCompletionState showCompletionState)
    112.     {
    113.         //LoadAd();
    114.         switch (showCompletionState)
    115.         {
    116.             case UnityAdsShowCompletionState.COMPLETED:
    117.                 //showAd = false;
    118.                 break;
    119.             case UnityAdsShowCompletionState.SKIPPED:
    120.                 //showAd = false;
    121.                 break;
    122.             case UnityAdsShowCompletionState.UNKNOWN:
    123.                 Debug.LogWarning("Ad Failed");
    124.                 ShowDebug("Ad Failed");
    125.                 break;
    126.         }
    127.     }
    128.  
    129.     private void ShowDebug(string debugText)
    130.     {
    131.         string newText = debugLog.text;
    132.         if (countDebugLog == 0)
    133.         {
    134.             newText = debugText;
    135.         }
    136.         else if (countDebugLog < 12)
    137.         {
    138.             newText = String.Concat(newText, Environment.NewLine, debugText);
    139.         }
    140.         else
    141.         {
    142.             newText = debugText;
    143.             countDebugLog = 0;
    144.         }
    145.  
    146.         debugLog.text = newText;
    147.         ++countDebugLog;
    148.     }
    149.    
    150. }
     
  4. Unity_Adamski

    Unity_Adamski

    Unity Technologies

    Joined:
    Jul 20, 2020
    Posts:
    110
    Hi @hearnodarkness looking at your code I can see that you are calling Advertisement.Show without leaving sufficient time for the SDK to load an ad. That's why you are seeing the placement not ready error. Please ensure that you have loaded an ad completely before trying to display it.
     
  5. hearnodarkness

    hearnodarkness

    Joined:
    Nov 12, 2020
    Posts:
    4
    Hey friend, thank you for the answer. I changed for 10 minutes and even so I had the same exactly problem.
     
  6. Unity-Boon

    Unity-Boon

    Unity Technologies

    Joined:
    Jan 18, 2017
    Posts:
    135
    Hi, I have attached a sample script, can you test it out and check is it working for you?
     

    Attached Files:

  7. hearnodarkness

    hearnodarkness

    Joined:
    Nov 12, 2020
    Posts:
    4
    I changed one thing or other to make it like I want...
    I cannot believe... it works!!! I will put in production to confirm but it worked on APK!
    Thank you so much!!!!