Search Unity

Monetization coroutine error

Discussion in 'Unity Ads & User Acquisition' started by Chance-Touchstone, Feb 15, 2019.

  1. Chance-Touchstone

    Chance-Touchstone

    Joined:
    Dec 26, 2013
    Posts:
    43
    Hello,
    Following this (Implementing basic (non-rewarded) ads) Monetization example, I get an error on StartCoroutine(ShowAdWhenReady());

    Error: unityengine.monobehaviour.startcoroutine nullreferenceException monobehaviourbinings.gen.cs

    Can anyone tell me why? Or what that means?


    If I call the following directly without the coroutine it works. but ShowAdWhenReady() as is in the example errors.

    //This works:
    Code (CSharp):
    1.  
    2.  
    3.   public void ShowAd()
    4.     {
    5.         ShowAdPlacementContent ad = null;
    6.         ad = Monetization.GetPlacementContent(placementId) as ShowAdPlacementContent;
    7.  
    8.         if (ad != null)
    9.         {
    10.             ad.Show();
    11.         }
    12. }
    13.  

    //This does not:
    Code (CSharp):
    1.  
    2.     public void ShowAd()
    3.     {
    4.         StartCoroutine(ShowAdWhenReady());
    5.     }
    6.  
    7.     private IEnumerator ShowAdWhenReady () {
    8.        while (!Monetization.IsReady (placementId)) {
    9.            yield return new WaitForSeconds(0.25f);
    10.        }
    11.  
    12.        ShowAdPlacementContent ad = null;
    13.        ad = Monetization.GetPlacementContent (placementId) as ShowAdPlacementContent;
    14.  
    15.        if(ad != null) {
    16.            ad.Show ();
    17.        }
    18.    }
     
  2. ZKapps

    ZKapps

    Joined:
    Nov 9, 2016
    Posts:
    4
    it's wired for me too.
    I think you can use what is working on Editor but on mobile you have to use coroutine
     
  3. Chance-Touchstone

    Chance-Touchstone

    Joined:
    Dec 26, 2013
    Posts:
    43
    So use co-routine when on mobile and non-co-routine in editor? I guess I could try that. I didn't think to test it on a mobile device. I'll post if that works on mobile. I'm still up for any suggestions to fix it too.

    Update:
    The Ads coroutine still didn't work on Android. =(
     
    Last edited: Feb 25, 2019
  4. StartStart

    StartStart

    Joined:
    Jan 2, 2013
    Posts:
    150
    Did you call initialize(gameId,false) function that Unity tutorial said?

    then if you ads show perfectly fine in Unity Editor it's okay to use ( Do not forget to turn off the test ).

    another way
    try to use if(Adverstise.isSupport) and if(Monetize.isSupport) first for checking devices can be show ads.
     
  5. Chance-Touchstone

    Chance-Touchstone

    Joined:
    Dec 26, 2013
    Posts:
    43
    Hi TARTS,
    Yes, I'm initializing it. I'm starting to think it's related the calling method being static?! Here's a snippet of my code with a few notes. I've tried many variations of this with the same results. My ads object is null but it still steps into the ShowAd() method when debugging where it initializes the ads before it errors on the StartCoroutine(ShowAdWhenReady()); line.

    Code (CSharp):
    1.  public class SCR_main : MonoBehaviour
    2.     {
    3.  
    4.         public static void OptionAction(int com)
    5.         {
    6.              //do things... then show the ad.
    7.  
    8.  
    9.             SCR_AdManager ads = new SCR_AdManager();
    10.  
    11.             //When debugging, ads is null at this point even though I just instantiated it.
    12.             //But I can still step into the ShowAd() method.
    13.             ads.ShowAd();
    14.  
    15.         }
    16.     }
    17.  
    Code (CSharp):
    1.  public class SCR_AdManager : MonoBehaviour
    2.     {
    3.         public void InitializeAds()
    4.         {
    5.             string gameID = "123456"; //Of course with a REAL game ID.
    6.             Monetization.Initialize(gameID, false);
    7.         }
    8.  
    9.         public void ShowAd()
    10.         {
    11.             if (!Monetization.Monetization)
    12.             {
    13.                 InitializeAds();
    14.             }
    15.  
    16.             //At this point Monetization.Monetization is true
    17.  
    18.             //A nullreferenceexception error is thrown here when attempting to start the coroutine. ???
    19.             StartCoroutine(ShowAdWhenReady());
    20.         }
    21.  
    22.         private IEnumerator ShowAdWhenReady()
    23.         {
    24.            //This is never called due to error on StartCoroutine.
    25.  
    26.             while (!Monetization.IsReady(placementId))
    27.             {
    28.                 yield return new WaitForSeconds(0.25f);
    29.             }
    30.  
    31.             ShowAdPlacementContent ad = null;
    32.             ad = Monetization.GetPlacementContent(placementId) as ShowAdPlacementContent;
    33.  
    34.             if (ad != null)
    35.             {
    36.                 ad.Show();
    37.             }
    38.         }
    39.     }
    40.  
    Any ideas?
     
    Last edited: Feb 26, 2019
  6. StartStart

    StartStart

    Joined:
    Jan 2, 2013
    Posts:
    150
    Try instantitate GameObject with SCR_AdManager not just new class.
     
    Chance-Touchstone likes this.
  7. Chance-Touchstone

    Chance-Touchstone

    Joined:
    Dec 26, 2013
    Posts:
    43
    YES! That fixed it! I didn't realize I needed to instantiate a game object for that.
    Thank you!
     
    Last edited: Feb 28, 2019