Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Question I switched from ads 3.7 to ads 4.0, I get the error "IsReady" and "resultcallback"

Discussion in 'Unity Ads & User Acquisition' started by MbGamesss, Oct 2, 2023.

  1. MbGamesss

    MbGamesss

    Joined:
    Sep 19, 2023
    Posts:
    5
    Hello
    I switched from ads 3.7 to ads 4.0 i take this erors What should I change in my code to fix it? Can you share the corrected codes?

    Codes

    Code (CSharp):
    1. #if CHARTBOOST_ADS
    2. using ChartboostSDK;
    3. #endif
    4. using System;
    5. using System.Collections.Generic;
    6. using SweetSugar.Scripts.Core;
    7. using SweetSugar.Scripts.Integrations;
    8. using UnityEngine;
    9. #if GOOGLE_MOBILE_ADS
    10. using GoogleMobileAds.Api;
    11.  
    12. #endif
    13. #if UNITY_ADS
    14. using UnityEngine.Advertisements;
    15. #endif
    16. namespace SweetSugar.Scripts.AdsEvents
    17. {
    18.     /// <summary>
    19.     /// Ads manager responsible for initialization and showing
    20.     /// </summary>
    21.     public class AdsManager : UnityEngine.MonoBehaviour
    22.     {
    23.         public static AdsManager THIS;
    24.         //EDITOR: ads events
    25.         public List<AdEvents> adsEvents = new List<AdEvents>();
    26.         //is unity ads enabled
    27.         public bool enableUnityAds;
    28.         //is admob enabled
    29.         public bool enableGoogleMobileAds;
    30.         //is chartboost enabled
    31.         public bool enableChartboostAds;
    32.         //rewarded zone for Unity ads
    33.         public string rewardedVideoZone;
    34.         //admob stuff
    35. #if GOOGLE_MOBILE_ADS
    36.         public InterstitialAd interstitial;
    37.     private AdRequest requestAdmob;
    38. #endif
    39.         public string admobUIDAndroid;
    40.         public string admobRewardedUIDAndroid;
    41.         public string admobUIDIOS;
    42.         public string admobRewardedUIDIOS;
    43.         private AdManagerScriptable adsSettings;
    44.  
    45.         private void Awake()
    46.         {
    47.             if (THIS == null) THIS = this;
    48.             else if(THIS != this)
    49.             {
    50.                 Destroy(gameObject);
    51.                 return;
    52.             }
    53.             DontDestroyOnLoad(this);
    54.             adsSettings = Resources.Load<AdManagerScriptable>("Scriptable/AdManagerScriptable");
    55.             adsEvents = adsSettings.adsEvents;
    56.             admobUIDAndroid = adsSettings.admobUIDAndroid;
    57.             admobUIDIOS = adsSettings.admobUIDIOS;
    58. #if UNITY_ADS//2.1.1
    59.             enableUnityAds = true;
    60.             var unityAds = Resources.Load<UnityAdsID>("Scriptable/UnityAdsID");
    61.         #if UNITY_ANDROID
    62.             Advertisement.Initialize(unityAds.androidID,false);
    63.         #elif UNITY_IOS
    64.             Advertisement.Initialize(unityAds.iOSID,false);
    65.         #endif
    66. #else
    67.         enableUnityAds = false;
    68. #endif
    69. #if CHARTBOOST_ADS//1.6.1
    70.         enableChartboostAds = true;
    71. #else
    72.             enableChartboostAds = false;
    73. #endif
    74. #if GOOGLE_MOBILE_ADS
    75.         enableGoogleMobileAds =true;//1.6.1
    76. #if UNITY_ANDROID
    77.         MobileAds.Initialize(admobUIDAndroid);//2.1.6
    78.         interstitial = new InterstitialAd(admobUIDAndroid);
    79. #elif UNITY_IOS
    80.         MobileAds.Initialize(admobUIDIOS);//2.1.6
    81.         interstitial = new InterstitialAd(admobUIDIOS);
    82. #else
    83.         MobileAds.Initialize(admobUIDAndroid);//2.1.6
    84.         interstitial = new InterstitialAd (admobUIDAndroid);
    85. #endif
    86.  
    87.         // Create an empty ad request.
    88.         requestAdmob = new AdRequest.Builder().Build();
    89.         // Load the interstitial with the request.
    90.         interstitial.LoadAd(requestAdmob);
    91.         interstitial.OnAdLoaded += HandleInterstitialLoaded;
    92.         interstitial.OnAdFailedToLoad += HandleInterstitialFailedToLoad;
    93. #else
    94.             enableGoogleMobileAds = false;//1.6.1
    95. #endif
    96.         }
    97.        
    98. #if GOOGLE_MOBILE_ADS
    99.    
    100.     public void HandleInterstitialLoaded(object sender, EventArgs args)
    101.     {
    102.         print("HandleInterstitialLoaded event received.");
    103.     }
    104.  
    105.     public void HandleInterstitialFailedToLoad(object sender, AdFailedToLoadEventArgs args)
    106.     {
    107.         print("HandleInterstitialFailedToLoad event received with message: " + args.Message);
    108.     }
    109. #endif
    110.        
    111.         public bool GetRewardedUnityAdsReady()
    112.         {
    113. #if APPODEAL
    114.         return AppodealIntegration.THIS.IsRewardedLoaded();
    115. #endif
    116. #if UNITY_ADS
    117.  
    118.             rewardedVideoZone = "rewardedVideo";
    119.             if (Advertisement.IsReady(rewardedVideoZone))
    120.             {
    121.                 return true;
    122.             }
    123.             else
    124.             {
    125.                 rewardedVideoZone = "rewardedVideoZone";
    126.                 if (Advertisement.IsReady(rewardedVideoZone))
    127.                 {
    128.                     return true;
    129.                 }
    130.             }
    131. #endif
    132.  
    133.             return false;
    134.         }
    135.  
    136.         private void OnDisable()
    137.         {
    138. #if GOOGLE_MOBILE_ADS
    139.             if (interstitial != null)
    140.             {
    141.                 interstitial.OnAdLoaded -= HandleInterstitialLoaded;
    142.                 interstitial.OnAdFailedToLoad -= HandleInterstitialFailedToLoad;
    143.             }
    144.  
    145. #endif
    146.         }
    147.  
    148.         public delegate void RewardedShown();
    149.  
    150.         public static event RewardedShown OnRewardedShown;
    151.         public void ShowRewardedAds()
    152.     {
    153. #if APPODEAL
    154.         Debug.Log("show Rewarded ads video in " + LevelManager.THIS.gameStatus);
    155.  
    156.         if (GetRewardedUnityAdsReady())
    157.         {
    158.             AppodealIntegration.THIS.ShowRewardedAds();
    159.         }
    160.         else{
    161.             #if UNITY_ADS
    162.             Advertisement.Show(rewardedVideoZone, new ShowOptions
    163.             {
    164.                 resultCallback = result =>
    165.                 {
    166.                     if (result == ShowResult.Finished)
    167.                     {
    168.                         OnRewardedShown?.Invoke();
    169.                         InitScript.Instance.ShowReward();
    170.                     }
    171.                 }
    172.             });
    173.             #endif
    174.         }
    175. #elif UNITY_ADS
    176.         Debug.Log("show Rewarded ads video in " + LevelManager.THIS.gameStatus);
    177.  
    178.         if (GetRewardedUnityAdsReady())
    179.         {
    180.             #if UNITY_ADS
    181.             Advertisement.Show(rewardedVideoZone, new ShowOptions
    182.             {
    183.                 resultCallback = result =>
    184.                 {
    185.                     if (result == ShowResult.Finished)
    186.                     {
    187.                         OnRewardedShown?.Invoke();
    188.                         InitScript.Instance.ShowReward();
    189.                     }
    190.                 }
    191.             });
    192.             #endif
    193.         }
    194. //#elif GOOGLE_MOBILE_ADS//2.2
    195. //        bool stillShow = true;
    196. //#if UNITY_ADS
    197. //        stillShow = !GetRewardedUnityAdsReady ();
    198. //#endif
    199. //        if(stillShow)
    200. //        {
    201. //            Debug.Log("show Admob Rewarded ads video in " + LevelManager.THIS.gameStatus);
    202. //            RewAdmobManager.THIS.ShowRewardedAd(CheckRewardedAds);
    203. //        }
    204. #endif
    205.     }
    206.  
    207.     public void CheckAdsEvents(GameState state)
    208.     {  
    209.         foreach (var item in adsEvents)
    210.         {
    211.             if (item.gameEvent == state)
    212.             {
    213.                 item.calls++;
    214.                 if (item.calls % item.everyLevel == 0)
    215.                     ShowAdByType(item.adType);
    216.             }
    217.         }
    218.     }
    219.  
    220.     void ShowAdByType(AdType adType)
    221.     {
    222.         if (adType == AdType.AdmobInterstitial && enableGoogleMobileAds)
    223.             ShowAds(false);
    224.         else if (adType == AdType.UnityAdsVideo && enableUnityAds)
    225.             ShowVideo();
    226.         else if (adType == AdType.ChartboostInterstitial && enableChartboostAds)
    227.             ShowAds(true);
    228.         else if(adType == AdType.Appodeal)
    229.             ShowAds(false);
    230.     }
    231.  
    232.     public void ShowVideo()
    233.     {
    234. #if UNITY_ADS
    235.         Debug.Log("show Unity ads video in " + LevelManager.THIS.gameStatus);
    236.  
    237.         if (Advertisement.IsReady("video"))
    238.         {
    239.             Advertisement.Show("video");
    240.         }
    241.         else
    242.         {
    243.             if (Advertisement.IsReady("defaultZone"))
    244.             {
    245.                 Advertisement.Show("defaultZone");
    246.             }
    247.         }
    248. #endif
    249.     }
    250.  
    251.     public void CacheRewarded()
    252.     {
    253. #if APPODEAL
    254.         AppodealIntegration.THIS.RequestRewarded();
    255. #endif
    256.  
    257.     }
    258.  
    259.  
    260.     public void ShowAds(bool chartboost = true)
    261.     {
    262.         #if APPODEAL
    263.         if(AppodealIntegration.THIS.IsInterstitialLoaded())
    264.         {
    265.             Debug.Log("show  Interstitial in " + LevelManager.THIS.gameStatus);
    266.             AppodealIntegration.THIS.ShowInterstitial();
    267.         }
    268.         #endif
    269.         if (chartboost)
    270.         {
    271. #if CHARTBOOST_ADS
    272.             Debug.Log("show Chartboost Interstitial in " + LevelManager.THIS.gameStatus);
    273.  
    274.             Chartboost.showInterstitial(CBLocation.Default);
    275.             Chartboost.cacheInterstitial(CBLocation.Default);
    276. #endif
    277.         }
    278.         else
    279.         {
    280. #if GOOGLE_MOBILE_ADS
    281.             Debug.Log("show admob Interstitial in " + LevelManager.THIS.gameStatus);
    282.             if (interstitial.IsLoaded())
    283.             {
    284.                 interstitial.Show();
    285. #if UNITY_ANDROID
    286.                 interstitial = new InterstitialAd(admobUIDAndroid);
    287. #elif UNITY_IOS
    288.                 interstitial = new InterstitialAd(admobUIDIOS);
    289. #else
    290.                 interstitial = new InterstitialAd (admobUIDAndroid);
    291. #endif
    292.  
    293.                 // Create an empty ad request.
    294.                 requestAdmob = new AdRequest.Builder().Build();
    295.                 // Load the interstitial with the request.
    296.                 interstitial.LoadAd(requestAdmob);
    297.             }
    298. #endif
    299.         }
    300.     }
    301.     }
    302. }
    Erors

    upload_2023-10-2_16-40-57.png

    Önem Derecesi Kod Açıklama Proje Dosya Çizgi Gizleme Durumu
    Hata CS0117 'Advertisement' bir 'IsReady' tanımı içermiyor Assembly-CSharp C:\Users\Mert\Desktop\SweetCandyMatch3\Assets\SweetSugar\Scripts\AdsEvents\AdsManager.cs 119 Etkin
    Hata CS0117 'Advertisement' bir 'IsReady' tanımı içermiyor Assembly-CSharp C:\Users\Mert\Desktop\SweetCandyMatch3\Assets\SweetSugar\Scripts\AdsEvents\AdsManager.cs 126 Etkin
    Hata CS0117 'ShowOptions' bir 'resultCallback' tanımı içermiyor Assembly-CSharp C:\Users\Mert\Desktop\SweetCandyMatch3\Assets\SweetSugar\Scripts\AdsEvents\AdsManager.cs 183 Etkin
    Hata CS0117 'Advertisement' bir 'IsReady' tanımı içermiyor Assembly-CSharp C:\Users\Mert\Desktop\SweetCandyMatch3\Assets\SweetSugar\Scripts\AdsEvents\AdsManager.cs 237 Etkin
    Hata CS0117 'Advertisement' bir 'IsReady' tanımı içermiyor Assembly-CSharp C:\Users\Mert\Desktop\SweetCandyMatch3\Assets\SweetSugar\Scripts\AdsEvents\AdsManager.cs 243 Etkin
    upload_2023-10-2_16-40-57.png
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    39,015
    Start with the documentation to see how the API in question may have changed.

    Remember: NOBODY here memorizes error codes. That's not a thing. The error code is absolutely the least useful part of the error. It serves no purpose at all. Forget the error code. Put it out of your mind.

    The complete error message contains everything you need to know to fix the error yourself.

    The important parts of the error message are:

    - the description of the error itself (google this; you are NEVER the first one!)
    - the file it occurred in (critical!)
    - the line number and character position (the two numbers in parentheses)
    - also possibly useful is the stack trace (all the lines of text in the lower console window)

    Always start with the FIRST error in the console window, as sometimes that error causes or compounds some or all of the subsequent errors. Often the error will be immediately prior to the indicated line, so make sure to check there as well.

    Look in the documentation. Every API you attempt to use is probably documented somewhere. Are you using it correctly? Are you spelling it correctly? Are you structuring the syntax correctly? Look for examples!

    All of that information is in the actual error message and you must pay attention to it. Learn how to identify it instantly so you don't have to stop your progress and fiddle around with the forum.
     
    Yasuyuki likes this.
  3. MbGamesss

    MbGamesss

    Joined:
    Sep 19, 2023
    Posts:
    5
    Can you share the corrected codes with me?
     
  4. Yasuyuki

    Yasuyuki

    Unity Technologies

    Joined:
    Sep 11, 2014
    Posts:
    155
  5. MbGamesss

    MbGamesss

    Joined:
    Sep 19, 2023
    Posts:
    5
    Can you sharefixxed codes with me ?