Search Unity

Bug When ad must show, game crashes [Android, AdMob]

Discussion in 'Unity Ads & User Acquisition' started by DZZc0rd, Feb 16, 2023.

  1. DZZc0rd

    DZZc0rd

    Joined:
    Sep 4, 2021
    Posts:
    59
    I'm making game for android with AdMob. In editor ad is showing, but on release on ad showing moment game crashes.

    Unity version: 2021.3.14f1
    AdMob version: 7.4.0

    Here code:

    Code (CSharp):
    1. using UnityEngine;
    2. using GoogleMobileAds.Api;
    3.  
    4. public class AdManager : MonoBehaviour
    5. {
    6.     [SerializeField] private string idAd;
    7.  
    8.     private int adShowedCounter = 0;
    9.     [SerializeField, Min(1)] private int adShowingIteration = 2;
    10.  
    11.     public static AdManager Instance = new AdManager();
    12.  
    13.     private InterstitialAd interstitial;
    14.  
    15.     public void ShowAd()
    16.     {
    17.         adShowedCounter++;
    18.  
    19.         if (interstitial != null)
    20.         {
    21.             interstitial.Destroy();
    22.             interstitial = null;
    23.         }
    24.  
    25.         if (adShowedCounter >= adShowingIteration)
    26.         {
    27.             InterstitialAd.Load(idAd, new AdRequest.Builder().Build(), (ad, err) =>
    28.             {
    29.                 if (err != null || ad == null)
    30.                 {
    31.                     Debug.LogError(err.GetMessage());
    32.  
    33.                     return;
    34.                 }
    35.  
    36.                 interstitial = ad;
    37.             });
    38.  
    39.             adShowedCounter = 0;
    40.         }
    41.  
    42.         if (interstitial != null && interstitial.CanShowAd())
    43.             interstitial.Show();
    44.         else
    45.             Debug.LogError("Advertisement is not ready");
    46.     }
    47. }
     
  2. SF_FrankvHoof

    SF_FrankvHoof

    Joined:
    Apr 1, 2022
    Posts:
    780
    Any Errors via LogCat? Only thing that stands out to me is a potential nullref in
    Debug.LogError(err.GetMessage());
    (if ad == null && err == null)
     
  3. DZZc0rd

    DZZc0rd

    Joined:
    Sep 4, 2021
    Posts:
    59
    Can you say how to use LogCat?
     
  4. SF_FrankvHoof

    SF_FrankvHoof

    Joined:
    Apr 1, 2022
    Posts:
    780
    1. Enable USB debugging on the device (under Developer Settings).
    2. Connect device to PC via USB
    3. Build & Run a debug-build
    4. open a command prompt, and run 'adb logcat'.
    this will give you a console-output for your device.

    Many videos for this on YouTube if this is hard to understand.
    Here's a random one: Realtime Debugging for Unity Android Apps - ADB LogCat Tutorial - YouTube
     
  5. DZZc0rd

    DZZc0rd

    Joined:
    Sep 4, 2021
    Posts:
    59
    Here:

    Code (CSharp):
    1. 02-16 13:11:08.617 13524 13571 E Unity   : Advertisement is not ready
    2. 02-16 13:11:08.617 13524 13571 E Unity   : Puzzlemanager:SelectNextLevel()
    3. 02-16 13:11:08.617 13524 13571 E Unity   : UnityEngine.Events.UnityEvent:Invoke()
    4. 02-16 13:11:08.617 13524 13571 E Unity   : UnityEngine.EventSystems.ExecuteEvents:Execute(GameObject, BaseEventData, EventFunction`1)
    5. 02-16 13:11:08.617 13524 13571 E Unity   : UnityEngine.EventSystems.StandaloneInputModule:ProcessTouchPress(PointerEventData, Boolean, Boolean)
    6. 02-16 13:11:08.617 13524 13571 E Unity   : UnityEngine.EventSystems.StandaloneInputModule:ProcessTouchEvents()
    7. 02-16 13:11:08.617 13524 13571 E Unity   : UnityEngine.EventSystems.StandaloneInputModule:Process()
    8. 02-16 13:11:08.617 13524 13571 E Unity   :
     
  6. DZZc0rd

    DZZc0rd

    Joined:
    Sep 4, 2021
    Posts:
    59
    I forgot to write that ads should be displayed several times in one scene