- Home /
Why cannot I show Interstitial ads?
Hello everyone,
I have tried many things but I could not make interstitial ads display. 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);
}
}
Answer by auhan · Nov 29, 2015 at 10:48 PM
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 );
}
}
Answer by Karmate · Nov 28, 2015 at 11:23 PM
private void ShowInterstitial()
{
//if not loaded, do nothing!
if (interstitial.IsLoaded())
{
interstitial.Show();
}
}
Fixed Version:
void wrong()
{
StartCoroutine ("ShowAd");
Application.LoadLevel("Notice");
}
IEnumerator ShowAd ()
{
//wait until loaded
while (!interstitial.IsLoaded())
yield return null;
interstitial.Show ();
}
@$$anonymous$$armate Thank you for your response but it also does not work. :( I am gonna lose my $$anonymous$$d, in console both codes work perfectly, it says Dummy IsLoaded and Dummy ShowInterstitial but they do not display in my android phone.
Hey, this is my code and works perfect. Check it. You need to call Show() method.
I use this plugin. https://github.com/googleads/googleads-mobile-unity/releases
using UnityEngine;
using System.Collections;
using Google$$anonymous$$obileAds.Api;
public class Ads : $$anonymous$$onoBehaviour
{
string caPub = "ca-app-pub-6798468007925431/xxxxxxxxx";
InterstitialAd reklamObjesi;
public void Show ()
{
reklamObjesi = new InterstitialAd (caPub);
AdRequest reklamiAl = new AdRequest.Builder ().Build ();
reklamObjesi.LoadAd (reklamiAl);
StartCoroutine ("ReklamiGoster");
}
IEnumerator ReklamiGoster ()
{
while (!reklamObjesi.IsLoaded())
yield return null;
reklamObjesi.Show ();
}
}
Thank you my friend. You gave me a different idea and I found a new code that works like a charm!
Your answer
Follow this Question
Related Questions
admob plugin for ios and android 1 Answer
Is there a way to save the xcode settings after build? 4 Answers
How to add AdMob into unity game? 1 Answer
how to pop interstital once a day? 1 Answer
Admob banner is not showing 1 Answer