Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Assets/Game/Scripts/AdmobScript.cs(57,10): error CS0128: A local variable named `adUnitId' is alread

Discussion in 'Scripting' started by mspag, Dec 16, 2017.

  1. mspag

    mspag

    Joined:
    Dec 17, 2016
    Posts:
    1
    Can someone please help me with this error? I am new to coding and I am trying to implement all the ad types in my game and I am getting this one error and do not know how to fix it.



    Code (CSharp):
    1. using GoogleMobileAds.Api;
    2.  
    3. public class AdmobScript : MonoBehaviour
    4. {
    5.     InterstitialAd interstitial;
    6.     RewardBasedVideoAd rewardBasedVideo;
    7.  
    8.     public Theme unlockThis;
    9.     // Use this for initialization
    10.     void Start()
    11.     {
    12.         //Request Ads
    13.         RequestBanner();
    14.         RequestInterstitial();
    15.         RequestRewardBasedVideo();
    16.     }
    17.  
    18.    
    19.     private void RequestBanner()
    20.     {
    21.         #if UNITY_EDITOR
    22.             string adUnitId = "unused";
    23. #elif UNITY_ANDROID
    24.         string adUnitId = "
    25.        ca-app-pub-4054604182571249/2424725311";
    26.             string adUnitId = "unexpected_platform";
    27. #endif
    28.  
    29.         // Create a 320x50 banner at the bottom of the screen.
    30.         BannerView bannerView = new BannerView(adUnitId, AdSize.Banner, AdPosition.Top);
    31.         // Create an empty ad request.
    32.         AdRequest request = new AdRequest.Builder().Build();
    33.         // Load the banner with the request.
    34.         bannerView.LoadAd(request);
    35.     }
    36.  
    37.     private void RequestRewardBasedVideo()
    38.     {
    39. #if UNITY_EDITOR
    40.         string adUnitId = "unused";
    41. #elif UNITY_ANDROID
    42.         string adUnitId = "ca-app-pub-4054604182571249/1265301285";
    43.         string adUnitId = "unexpected_platform";
    44. #endif
    45.         rewardBasedVideo = RewardBasedVideoAd.Instance;
    46.  
    47.         AdRequest request = new AdRequest.Builder().Build();
    48.         rewardBasedVideo.LoadAd(request, adUnitId);
    49.     }
    50.  
    51.     private void RequestInterstitial()
    52.     {
    53. #if UNITY_ANDROID
    54.         string adUnitId = "ca-app-pub-4054604182571249/1815965622";
    55.         string adUnitId = "unexpected_platform";
    56. #endif
    57.          // Initialize an InterstitialAd.
    58.         interstitial = new InterstitialAd(adUnitId);
    59.         // Create an empty ad request.
    60.         AdRequest request = new AdRequest.Builder().Build();
    61.         // Load the interstitial with the request.
    62.         interstitial.LoadAd(request);
    63.     }
    64.  
    65.     public void showInterstitialAd()
    66.     {
    67.         //Show Ad
    68.         if (interstitial.IsLoaded())
    69.         {
    70.             interstitial.Show();
    71.         }
    72.         else RequestInterstitial();
    73.  
    74.     }
    75.  
    76.  
    77.     public void ShowRewardedAd()
    78.     {
    79.         if (rewardBasedVideo.IsLoaded() && !unlockThis.isBought)
    80.         {
    81.             rewardBasedVideo.OnAdRewarded += HandleRewardBasedVideoRewarded;
    82.             rewardBasedVideo.Show();
    83.         }
    84.         else RequestRewardBasedVideo();
    85.     }
    86.  
    87.     public void HandleRewardBasedVideoRewarded(object sender, Reward args)
    88.     {
    89.         unlockThis.PurchaseSuccessful();
    90.     }
    91.  
    92.     public void ExitApp()
    93.     {
    94.         interstitial.OnAdClosed += HandleOnQuit;
    95.         interstitial.OnAdFailedToLoad += HandleOnQuit;
    96.         showInterstitialAd();
    97.     }
    98.  
    99.     public void HandleOnQuit(object sender, System.EventArgs args)
    100.     {
    101.         Application.Quit();
    102.     }
    103.  
    104. }
     
  2. Fido789

    Fido789

    Joined:
    Feb 26, 2013
    Posts:
    343
    You cannot declare variable with same name twice

    Code (CSharp):
    1. string adUnitId = "ca-app-pub-4054604182571249/1265301285";
    2. string adUnitId = "unexpected_platform";
     
  3. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    I believe for each 2nd entry of 2, or 3rd entry of 3, there's a missing #else preprocessor statement.