Search Unity

Question [Admob] Problem with Interstitial class `InterstitialAd' does not contain a constructor

Discussion in 'Scripting' started by Ganicuus, Feb 2, 2023.

  1. Ganicuus

    Ganicuus

    Joined:
    Jan 24, 2021
    Posts:
    6
    Hi,
    I'm trying to add ads to my project, I followed this documentation.
    https://developers.google.com/admob/unity/interstitial

    I downloaded the classes and inserted them into the project via .unitypackage (I downloaded it from the link given in the documentation), but probably the class 'InterstitialAd' is modified and does not have a constructor as in the documentation.

    Here is the mentioned class:


    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.Advertisements;
    3.  
    4. public class InterstitialAd : MonoBehaviour, IUnityAdsLoadListener, IUnityAdsShowListener
    5. {
    6.     [SerializeField] string _androidAdUnitId = "Interstitial_Android";
    7.     [SerializeField] string _iOsAdUnitId = "Interstitial_iOS";
    8.     string _adUnitId;
    9.  
    10.     void Awake()
    11.     {
    12.         _adUnitId = (Application.platform == RuntimePlatform.IPhonePlayer) ? _iOsAdUnitId : _androidAdUnitId;
    13.  
    14.         LoadAd();
    15.     }
    16.  
    17.     public void LoadAd()
    18.     {
    19.         Debug.Log("Loading Ad: " + _adUnitId);
    20.         Advertisement.Load(_adUnitId, this);
    21.     }
    22.  
    23.     public void ShowAd()
    24.     {
    25.  
    26.        // if (FirebaseManager.instance.adStatusInt == 0)
    27.         Debug.Log("Showing Ad: " + _adUnitId);
    28.         Advertisement.Show(_adUnitId, this);
    29.  
    30.         LoadAd();
    31.     }
    32.  
    33.     public void OnUnityAdsAdLoaded(string adUnitId) { }
    34.  
    35.     public void OnUnityAdsFailedToLoad(string adUnitId, UnityAdsLoadError error, string message)
    36.     {
    37.         Debug.Log($"Error loading Ad Unit: {adUnitId} - {error.ToString()} - {message}");
    38.     }
    39.  
    40.     public void OnUnityAdsShowFailure(string adUnitId, UnityAdsShowError error, string message)
    41.     {
    42.         Debug.Log($"Error showing Ad Unit {adUnitId}: {error.ToString()} - {message}");
    43.     }
    44.  
    45.     public void OnUnityAdsShowStart(string adUnitId) { }
    46.     public void OnUnityAdsShowClick(string adUnitId) { }
    47.     public void OnUnityAdsShowComplete(string adUnitId, UnityAdsShowCompletionState showCompletionState) { }
    48. }

    And here is the class I actually copied from the documentation and it should be fine:


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using System;
    4. using UnityEngine;
    5. using GoogleMobileAds.Api;
    6.  
    7. public class InterstitialGoAdd : MonoBehaviour
    8. {
    9.     private InterstitialAd interstitial;
    10.     void Start()
    11.     {
    12.         MobileAds.Initialize(initStatus => { });
    13.         RequestInterstitial();
    14.  
    15.        
    16.         if (this.interstitial.IsLoaded())
    17.         {
    18.             this.interstitial.Show();
    19.         }
    20.     }
    21.  
    22.     private void RequestInterstitial()
    23.     {
    24. #if UNITY_ANDROID
    25.         string adUnitId = "ca-app-pub-3940256099942544/1033173712";
    26. #elif UNITY_IPHONE
    27.         string adUnitId = "ca-app-pub-3940256099942544/4411468910";
    28. #else
    29.         string adUnitId = "unexpected_platform";
    30. #endif
    31.  
    32.         // Initialize an InterstitialAd.
    33.         this.interstitial = new InterstitialAd(adUnitId);
    34.         // Create an empty ad request.
    35.         AdRequest request = new AdRequest.Builder().Build();
    36.         // Load the interstitial with the request.
    37.         this.interstitial.LoadAd(request);
    38.     }
    39. }
    40.  

    And the error is "Assets\InterstitialGoAdd.cs(16,31): error CS1061: 'InterstitialAd' does not contain a definition for 'IsLoaded'...".
    Does anyone know what to do with this? I'd appreciate any advice/help.

    Thanks a lot.
     
  2. SF_FrankvHoof

    SF_FrankvHoof

    Joined:
    Apr 1, 2022
    Posts:
    780
    The InterstitialAd-class you've posted is from Unity-Ads, not AdMob.
    Not sure how you got the 2 mixed up/combined, but you did.
     
  3. Ganicuus

    Ganicuus

    Joined:
    Jan 24, 2021
    Posts:
    6
    Oh ye I see, my bad. I was using also UnityAds but I transfer to AdMob. However, I already figured out my problem, as I wrote there really was no constructor in the class. The documentation that is provided is for version 7.3.1 and below, so for a 2 week new release they don't have the documentation and they completely changed it. So the solution at this point is to switch to 7.3.1, or try to get out of the new classes without documentation.

    At least this helped me.