Search Unity

Unable to initialize Admob "MobileAds doesnt exist in the current context"

Discussion in 'Unity Ads' started by FaisalNaseer2k4, Sep 12, 2019.

  1. FaisalNaseer2k4

    FaisalNaseer2k4

    Joined:
    Aug 22, 2019
    Posts:
    6
    I am trying to initialize my Admob for release build in one of my unity app but as soon as I try to call
    Code (CSharp):
    1. MobileAds.Initialize()
    .

    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections.Generic;
    4. using System.Collections;
    5. using System;
    6. using UnityEngine.SocialPlatforms;
    7. using GoogleMobileAds.Api;
    8. using GoogleMobileAds;
    9. public class AdsControl : MonoBehaviour
    10. {
    11.    
    12.    
    13.     protected AdsControl ()
    14.     {
    15.     }
    16.  
    17.  
    18. [LIST=1]
    19. [*]
    20. [/LIST]
    21.  
    22.     private static AdsControl _instance;
    23.  
    24.     InterstitialAd interstitial;
    25.     BannerView bannerView;
    26.  
    27.     public string AdmobID_Android_Interstitial, AdmobID_IOS_Interstitial, AdmobID_Android_Banner, AdmobID_IOS_Banner,ADID;
    28.  
    29.     public static AdsControl Instance { get { return _instance; } }
    30.  
    31.     void Awake ()
    32.     {
    33.        
    34.         if (FindObjectsOfType (typeof(AdsControl)).Length > 1) {
    35.             Destroy (gameObject);
    36.             return;
    37.         }
    38.        
    39.         _instance = this;
    40. // issue is here
    41.        MobileAds.Initialize(ADID);
    42.         MakeNewInterstial ();
    43.         RequestBanner ();
    44.         ShowBanner ();
    45.        
    46.         DontDestroyOnLoad (gameObject); //Already done by CBManager
    47.  
    48.     }
    49.  
    50.  
    51.     public void HandleInterstialAdClosed (object sender, EventArgs args)
    52.     {
    53.  
    54.         MobileAds.Initialize();
    55.         if (interstitial != null)
    56.             interstitial.Destroy ();
    57.         MakeNewInterstial ();
    58.    
    59.    
    60.        
    61.     }
    62.  
    63.     void MakeNewInterstial ()
    64.     {
    65.        
    66.  
    67. #if UNITY_ANDROID
    68.         interstitial = new InterstitialAd (AdmobID_Android_Interstitial);
    69. #endif
    70. #if UNITY_IPHONE
    71.         interstitial = new InterstitialAd (AdmobID_IOS_Interstitial);
    72. #endif
    73.         interstitial.OnAdClosed += HandleInterstialAdClosed;
    74.         AdRequest request = new AdRequest.Builder ().Build ();
    75.         interstitial.LoadAd (request);
    76.  
    77.     }
    78.  
    79.  
    80.     public void showAds ()
    81.     {
    82.  
    83.         int gameOver = PlayerPrefs.GetInt ("GameOver");
    84.         if (gameOver < 2)
    85.             gameOver++;
    86.         else {
    87.             gameOver = 0;
    88.             interstitial.Show ();
    89.         }
    90.         PlayerPrefs.SetInt ("GameOver", gameOver);
    91.    
    92.  
    93.     }
    94.  
    95.     public void MoreGame ()
    96.     {
    97.        
    98.         interstitial.Show ();
    99.  
    100.     }
    101.  
    102.     private void RequestBanner ()
    103.     {
    104.        
    105.         string adUnitId = "";
    106.  
    107.         #if UNITY_ANDROID
    108.         adUnitId = AdmobID_Android_Banner;
    109.         #endif
    110.         #if UNITY_IPHONE
    111.         adUnitId = AdmobID_IOS_Banner;
    112.         #endif
    113.         // Create a 320x50 banner at the top of the screen.
    114.         bannerView = new BannerView (adUnitId, AdSize.Banner, AdPosition.Bottom);
    115.         // Load a banner ad.
    116.         AdRequest request = new AdRequest.Builder ().Build ();
    117.         bannerView.LoadAd (request);
    118.     }
    119.  
    120.     private void ShowBanner ()
    121.     {
    122.         bannerView.Show ();
    123.     }
    124.  
    125.        
    126. }
     
  2. MrArcher

    MrArcher

    Joined:
    Feb 27, 2014
    Posts:
    51
    According to this page, there's an issue with Unity stripping out the assembly code at build time. The two solutions posted in the linked thread are to either:
    1. Add the following lines to GoogleMobileAdsClientFactory.cs:
      Code (CSharp):
      1. using UnityEngine.Scripting;
      2. [assembly: Preserve]
    2. Change the managed stripping level to Low or Disabled under Player Settings > Optimization > Managed Stripping Level
    If the above doesn't work, there's a couple of other solutions in the linked thread.