Search Unity

TypeLoadException Runtime Exception With Unity Ads

Discussion in 'Unity Ads & User Acquisition' started by MartinCA, Dec 30, 2014.

  1. MartinCA

    MartinCA

    Joined:
    Jan 1, 2012
    Posts:
    15
    Hey everyone,
    I'm having a bit of a trouble getting Unity Ads to work. I have installed the plugin through the Unity package from the asset store, but when ever I'm trying to run the game in the editor I get this exception.

    This is my current implementation:

    Code (CSharp):
    1.     public override void Initialize()
    2.     {
    3.         m_minInterval         = Config.GetValue<float>( "unityads-min-interval", m_minInterval );
    4.         m_minNavigations      = Config.GetValue<int>  ( "unityads-min-navigations", m_minNavigations );
    5.         m_interstitialScreens = Config.GetValue<Dictionary<string, bool>>( "unityads-interstitial-screens", null );
    6.  
    7. #if UNITY_ANDROID || UNITY_IOS
    8.         if ( Advertisement.isSupported )
    9.         {
    10.             Advertisement.allowPrecache = true;
    11.  
    12. #if UNITY_ANDROID
    13.             Advertisement.Initialize( m_androidGameId, m_testMode );
    14. #elif UNITY_IOS
    15.             Advertisement.Initialize( m_iosGameId, m_testMode );
    16. #endif
    17.         }
    18.         else
    19.         {
    20.             Debug.Log( "Advertisments not supported on current platform!" );
    21.         }
    22.  
    23. #endif
    24.  
    25.         m_navigationsFromLastAd = 0;
    26.         m_lastAdTimestamp       = Time.realtimeSinceStartup;
    27.  
    28.         SetReady();
    29.     }
    30.  
    31. ...
    32. ...
    33.  
    34.     private void TryShowInterstitial( string screenName )
    35.     {
    36.         if ( m_interstitialScreens != null && m_interstitialScreens.ContainsKey( screenName ) && m_interstitialScreens[ screenName ] == true )
    37.         {          
    38. #if UNITY_ANDROID || UNITY_IOS
    39.            
    40.             if ( Advertisement.isInitialized && Advertisement.isReady() && !Advertisement.isShowing )
    41.             {
    42.                 m_navigationsFromLastAd = 0;
    43.                 m_lastAdTimestamp = Time.realtimeSinceStartup;
    44.  
    45.                 Advertisement.Show( null, new ShowOptions { pause = true } );
    46.             }
    47. #else
    48.  
    49.             // Counter resets on non supported platforms for debugging purposes only
    50.             m_navigationsFromLastAd = 0;
    51.             m_lastAdTimestamp       = Time.realtimeSinceStartup;
    52.  
    53.             Debug.Log( "-- Interstitial will play here --" );
    54. #endif
    55.         }
    56.     }
    Setting build configuration to anything other than Android or iOS works as expected (well, everything is ifdef'd out, no surprise there). But as soon as I switch the target platform to either Android or iOS I get the following runtime exceptions as soon as a method containing reference to Advertisements is invoked:

    Calling Initialize():

    TypeLoadException: Could not load type 'UnityEngine.Advertisements.Advertisement' from assembly 'Assembly-CSharp'.
    Game.Update () (at Assets/Shared/Scripts/Game/Game.cs:74)


    And if I comment out the whole initialization block and left it reach TryShowInterstitial() I get this one:

    TypeLoadException: Could not load type 'UnityEngine.Advertisements.ShowOptions' from assembly 'Assembly-CSharp-firstpass, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null'.
    UnityAdsManager.OnMenuScreenShown (.MenuScreen screen) (at Assets/Shared/Scripts/UnityAdsManager.cs:81)
    MenuScreen.Show () (at Assets/Shared/Scripts/UI/MenuScreen.cs:46)
    MenuManager.SetScreen (System.String screen, Boolean resetNavigation) (at Assets/Shared/Scripts/UI/MenuManager.cs:98)
    ScreenIntro.Update () (at Assets/Scripts/Screens/ScreenIntro.cs:28)


    The odd part about it is it does not throw this exception at start up, but only when a method containing a reference to Advertisements is invoked - but even without any actual call to Advertisements (i.e. in the case of TryShowInterstitial() it will throw the exception as soon as the method is invoked, even if it were to fail the first conditional test and never actually call anything from Advertisements).

    Some additional specs:
    Using Unity 4.5.4f1.
    Using NGUI (relatively recent, don't think it's relevant in any way)
    Using Google Analytics official plugin (latest release as of writing this)
    API Compatibility level set to .NET 2.0 Subset
    Testing in editor, reproduces identically when target platform is either Android or iOS.

    Sorry for the wall of text :)

    Would appreciate any information or ideas on how to resolve this. So far Googling and searching this forum turned up nothing.

    Thanks!
     
  2. MartinCA

    MartinCA

    Joined:
    Jan 1, 2012
    Posts:
    15
    Updated Unity to latest stable release 4.6.1f1 and everything just magically resolved itself.
    Case close!
     
    unity-nikkolai likes this.