0
\$\begingroup\$

I have tried many things but I could not make interstitial ads display in Unity. I want to show interstitial ads after users select a wrong answer and before the score page ("Notice" scene) displays. Here is my code, I just copied relevant parts:

 using UnityEngine;
 using UnityEngine.UI;
 using System.Text;
 using System.Xml;
 using System.Collections;
 using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Xml.Serialization;
 using System.IO;
 using GoogleMobileAds;
 using GoogleMobileAds.Api;

 public class responder : MonoBehaviour
 {
     private InterstitialAd interstitial;

     private int gecis;

     public Text questionsorular;
     public Text responseA;
     public Text responseB;
     public Text responseC;
     public Text responseD;
     public Text infoResponses;
     public Text infoResponses1;
     public Text example;
     public Text dogrusayisi;

     private float corrects;
     private float questoesquestions;
     private float media;
     private int Notice;
 }

 void Start()
 {
     RequestInterstitial();
 }

 public void response(string alternative)
 {

     RequestInterstitial();

     if (alternative == "A")
     {
         if (responseA.text == infoResponses.text)
         {
             corrects += 1;
             nextQuestion();
         }
         else
         {
             Invoke("wrong", 1);
         }
     }

     else if (alternative == "B")
     {
         if (responseB.text == infoResponses.text)
         {
             corrects += 1;
             nextQuestion();
         }
         else
         {
             Invoke("wrong", 1);
         }
     }

     else if (alternative == "C")
     {
         if (responseC.text == infoResponses.text)
         {
             corrects += 1;
             nextQuestion();
         }
         else
         {
             Invoke("wrong", 1);
         }
     }

     else if (alternative == "D")
     {
         if (responseD.text == infoResponses.text)
         {
             corrects += 1;
             nextQuestion();
         }
         else
         {
             Invoke("wrong", 1);
         }
     }
 }

 void wrong()
 {
     ShowInterstitial();
     Application.LoadLevel("Notice");
 }

 private void RequestInterstitial()
 {
 #if UNITY_EDITOR
     string adUnitId = "unused";
 #elif UNITY_ANDROID
             string adUnitId = "MY ADS ID";
 #elif UNITY_IPHONE
             string adUnitId = "INSERT_IOS_INTERSTITIAL_AD_UNIT_ID_HERE";
 #else
             string adUnitId = "unexpected_platform";
 #endif

     // Create an interstitial.
     interstitial = new InterstitialAd(adUnitId);
     // Load an interstitial ad.
     interstitial.LoadAd(createAdRequest());
 }

 private AdRequest createAdRequest()
 {
     return new AdRequest.Builder()
             .AddTestDevice(AdRequest.TestDeviceSimulator)
             .AddTestDevice("0123456789ABCDEF0123456789ABCDEF")
             .AddKeyword("game")
             .SetGender(Gender.Male)
             .SetBirthday(new DateTime(1985, 1, 1))
             .TagForChildDirectedTreatment(false)
             .AddExtra("color_bg", "9B30FF")
             .Build();
 }

 private void ShowInterstitial()
 {
     if (interstitial.IsLoaded())
     {
         interstitial.Show();
     }
 }

Following code works perfectly for banner ads when I attached it to camera:

 using UnityEngine;
 using GoogleMobileAds.Api;

 public class Ads : MonoBehaviour
 {
     void Start()
     {
         BannerView adsObject = new BannerView(
                 "MY ADS ID", AdSize.SmartBanner, AdPosition.Bottom);
         AdRequest getAds = new AdRequest.Builder().Build();
         adsObject.LoadAd(getAds);
         }
 }
\$\endgroup\$
1
  • \$\begingroup\$ Have you ran this in a debugger to ensure that you get to the appropriate spot? Or used any other means (print("in wrong"), etc) to ensure that you are going on the appropriate paths? Where is it getting stuck? \$\endgroup\$ – PearsonArtPhoto Nov 29 '15 at 0:43
0
\$\begingroup\$

Let's break this down. In your response function, you are doing two relevant things:

RequestInterstitial();
Invoke("wrong", 1);

I suspect what is happening is the first statement is trying to load a new ad, while the second one waits 1 second to display. If the network connection is within 1 second, the ad will display, otherwise, you won't see anything.

To test this, try removing the RequestInterstitial from the response function. It should work once. Then when you update the display, call RequestInterstitial to ensure that there is plenty of time to download the ad.

\$\endgroup\$
5
  • \$\begingroup\$ Dear @PearsonArtPhoto , I followed your advice and changed Invoke("wrong", 1); to wrong (); but unfortunately, it still does not work. In console, both codes work perfectly, it says Dummy IsLoaded and Dummy ShowInterstitial but they do not display in my android phone. \$\endgroup\$ – auhan Nov 29 '15 at 18:05
  • \$\begingroup\$ The problem isn't so much the call to wrong, but the RequestInterstitial. Remove that, and I think it'll work at least once. \$\endgroup\$ – PearsonArtPhoto Nov 29 '15 at 19:05
  • \$\begingroup\$ Thank you for your effort, @PearsonArtPhoto . I appreciate it but I finally found a code that works. \$\endgroup\$ – auhan Nov 29 '15 at 22:49
  • \$\begingroup\$ It seems your answer is at its root the same as mine, don't request the ad in the response function \$\endgroup\$ – PearsonArtPhoto Nov 29 '15 at 22:51
  • \$\begingroup\$ Yes, you are right but thanks to this code, advertisement displays every time after selecting the wrong answer. Thank you again. :-) \$\endgroup\$ – auhan Nov 29 '15 at 23:11
0
\$\begingroup\$

I finally did it!

 using UnityEngine;
 using UnityEngine.UI;
 using System.Text;
 using System.Xml;
 using System.Collections;
 using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Xml.Serialization;
 using System.IO;
 using GoogleMobileAds;
 using GoogleMobileAds.Api;

 public class responder : MonoBehaviour
 {
     private InterstitialAd adObject;

     private int gecis;

     public Text questionsorular;
     public Text responseA;
     public Text responseB;
     public Text responseC;
     public Text responseD;
     public Text infoResponses;
     public Text infoResponses1;
     public Text example;
     public Text dogrusayisi;

     private float corrects;
     private float questoesquestions;
     private float media;
     private int Notice;
 }

 void Start()
 {
     GetNewAds( null, null );
 }

 public void response(string alternative)
 {


     if (alternative == "A")
     {
         if (responseA.text == infoResponses.text)
         {
             corrects += 1;
             nextQuestion();
         }
         else
         {
             Invoke("wrong", 1);
         }
     }

     else if (alternative == "B")
     {
         if (responseB.text == infoResponses.text)
         {
             corrects += 1;
             nextQuestion();
         }
         else
         {
             Invoke("wrong", 1);
         }
     }

     else if (alternative == "C")
     {
         if (responseC.text == infoResponses.text)
         {
             corrects += 1;
             nextQuestion();
         }
         else
         {
             Invoke("wrong", 1);
         }
     }

     else if (alternative == "D")
     {
         if (responseD.text == infoResponses.text)
         {
             corrects += 1;
             nextQuestion();
         }
         else
         {
             Invoke("wrong", 1);
         }
     }
 }

 void wrong()
 {
     StartCoroutine(ShowAds());
     Application.LoadLevel("Notice");
 }

IEnumerator ShowAds()
    {
        while( !adObject.IsLoaded() )
            yield return null;

        adObject.Show();
    }

    public void GetNewAds( object sender, EventArgs args )
    {
        if( adObject != null )
            adObject.Destroy();

        adObject = new InterstitialAd( "AD UNIT ID" );
        adObject.AdClosed += GetNewAds;

        AdRequest GetAds = new AdRequest.Builder().Build();
        adObject.LoadAd( GetAds );
    }
}
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.