Search Unity

Google Admob interstitial ad not showing + please check my code

Discussion in 'Scripting' started by bence02, May 7, 2018.

  1. bence02

    bence02

    Joined:
    Nov 16, 2017
    Posts:
    9
    Dear Forum members!

    Please help me, I implemented the Unity Google Plugin with no errors. After I wrote the codes, and debugged, but the interstitial ad not showing. I don't understand what could be a problem. Thank you very much in advance!

    Here is my code:
    Code (csharp):
    1.  -----------
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. using GoogleMobileAds.Api;
    6. using System;
    7.  
    8.  
    9. public class admobstuff : MonoBehaviour {
    10.     public void Start()
    11.     {
    12.  #if UNITY_ANDROID
    13.                 string appId = "ca-app-pub-3940256099942544~3347511713";
    14. #elif UNITY_IPHONE
    15.                     string appId = "ca-app-pub-3940256099942544~1458002511";
    16. #else
    17.                     string appId = "unexpected_platform";
    18. #endif
    19.  
    20.         // Initialize the Google Mobile Ads SDK.
    21.         MobileAds.Initialize(appId);
    22.     }
    23.     string adUnitId = "ca-app-pub-3940256099942544/1033173712";
    24.  
    25.     private void RequestInterstitial()
    26.     {
    27.  #if UNITY_ANDROID
    28.                  adUnitId = "ca-app-pub-3940256099942544/1033173712";                                                            
    29. #elif UNITY_IPHONE
    30.                  adUnitId = "ca-app-pub-3940256099942544/4411468910";
    31. #else
    32.                  adUnitId = "unexpected_platform";
    33. #endif
    34.  
    35.         // Initialize an InterstitialAd.
    36.  
    37.     }
    38.  
    39.     InterstitialAd myadd = null;
    40.     public void CreateAnAd()
    41.     {
    42.         if (myadd != null)
    43.         {
    44.             myadd.Destroy();
    45.             myadd = null;
    46.         }
    47.  
    48.         RequestInterstitial();
    49.  
    50.         myadd = new InterstitialAd(adUnitId);
    51.         AdRequest request = new AdRequest.Builder().Build();
    52.         // Load the interstitial with the request.
    53.         myadd.LoadAd(request);
    54.  
    55.         if (myadd.IsLoaded())
    56.         {
    57.             myadd.Show();
    58.         }
    59.  
    60.     }
    61.  
    62.     public void OnGUI()
    63.     {
    64.         if(myadd != null)
    65.         {
    66.             float buttonWidth = 0.15f * Screen.width;
    67.             float buttonHeight = 0.15f * Screen.width;
    68.             Rect destroyInterstitialRect = new Rect(
    69.               0.01f * Screen.width,
    70.             0.01f * Screen.width,
    71.             buttonWidth,
    72.                buttonHeight);
    73.             if (GUI.Button(destroyInterstitialRect, "X"))
    74.             {
    75.                 this.myadd.Destroy();
    76.                 myadd = null;
    77.                 GameObject[] gameaa = GameObject.FindGameObjectsWithTag("WinsLoses");
    78.                 foreach (var item in gameaa)
    79.                 {
    80.                    if(item.GetComponent<WinTheGameWindow>() != null)
    81.                     {
    82.                         item.GetComponent<WinTheGameWindow>().DestroyedTheAd();
    83.                     }
    84.                     if (item.GetComponent<LostTheGameWindow>() != null)
    85.                     {
    86.                         item.GetComponent<LostTheGameWindow>().DestroyedTheAd();
    87.                     }
    88.                 }
    89.             }
    90.         }
    91.  
    92.  
    93.     }
    94. }
    95.  
    96.  
    97. ----------------------------------------
    98.  
     
    Last edited: May 7, 2018
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    4,970
unityunity