Admob Interstitial not showing
Hi, this code is probably wrong somewhere. No experience in C#... I' using Admob official plugin, but it doesn't work. I just want to show an interstital ad on load.... Thanks for any help!
public class GoogleMobileAdsDemoScript : MonoBehaviour {
private InterstitialAd interstitial;
private void RequestInterstitial()
{
#if UNITY_ANDROID
string adUnitId = "ca-app-pub-xxxx/xxxx";
#elif UNITY_IPHONE
string adUnitId = "INSERT_IOS_INTERSTITIAL_AD_UNIT_ID_HERE";
#else
string adUnitId = "unexpected_platform";
#endif
// Returns an ad request
private AdRequest CreateAdRequest()
{
return new AdRequest.Builder().Build();
}
// Create an interstitial.
this.interstitial = new InterstitialAd(adUnitId);
// Load an interstitial ad.
this.interstitial.LoadAd(this.CreateAdRequest());
}
private void ShowInterstitial() {
if (interstitial.IsLoaded()) {
interstitial.Show();
}}
public void Start()
{
ShowInterstitial();
}
}
Answer by TheRealMarco · Feb 25, 2017 at 07:16 PM
You need to enter here a proper id : string adUnitId = "ca-app-pub-xxxx/xxxx";
Like : string adUnitId = "ca-app-pub-3940256099942544/1033173712";
Which is a test id provided by Unity.
You also need to call RequestInterstiticial method before the ShowInterstitial method.
And you need to put CreateAdRequest() out of RequestInterstitial().
You need to call ShowInterstitial one time, when the ad is loaded. In this example, I call it 30 times per second, which is forbidden, but it's an exemple.
public class GoogleMobileAdsDemoScript : MonoBehaviour
{
private InterstitialAd interstitial;
private void RequestInterstitial()
{
#if UNITY_ANDROID
string adUnitId = "ca-app-pub-3940256099942544/1033173712";
#elif UNITY_IPHONE
string adUnitId = "INSERT_IOS_INTERSTITIAL_AD_UNIT_ID_HERE";
#else
string adUnitId = "unexpected_platform";
#endif
// Create an interstitial.
this.interstitial = new InterstitialAd(adUnitId);
// Load an interstitial ad.
this.interstitial.LoadAd(this.CreateAdRequest());
}
// Returns an ad request
private AdRequest CreateAdRequest()
{
return new AdRequest.Builder().Build();
}
private void ShowInterstitial()
{
if (interstitial.IsLoaded())
{
interstitial.Show();
}
}
public void Start()
{
RequestInterstitial();
}
void Update()
{
ShowInterstitial();
}
}
Answer by dyenaldinesh · Dec 17, 2017 at 07:11 AM
@TheRealMarco thank u so much..it really worked...everybody use this code..
I know this is quite an old thread, but I too am having issues with interstitials not loading.
I used the script above and it works by calling the ShowInterstitial() in the Update method, but I can't get it to work by calling it anywhere else.
Obviously we can't use it in the Update method, so does anyone have a way around it?
Your answer
Follow this Question
Related Questions
Build Failure with AdMob 0 Answers
Game doesn't work after adding AdMob 0 Answers
Google Mobile Ads cause Failed to build apk error 0 Answers
Gradle failed to fetch dependencies? 1 Answer
Android - Google Admob and Easy Mobile brakes build process 0 Answers