0
\$\begingroup\$

My ad does not appear. I want to click the button and have an ad appear.

using UnityEngine.SceneManagement;
using GoogleMobileAds.Api;

public class GameOverControl : MonoBehaviour {
  InterstitialAd interstitital;

  [SerializeField]
  private AudioSource OverSes;

  void Start () {
    OverSes.Play ();
    MobileAds.Initialize ("");

    RequestinterstitialAd ();
  }

  public void Tekrar () {
    SceneManager.LoadScene ("BonyBony");
    ShowinterstitialAd ();
  }

  public void MenuyeDon () {
    SceneManager.LoadScene ("MainMenu");
    ShowinterstitialAd ();
  }

  private void RequestinterstitialAd() {
    interstitital = new InterstitialAd ("");
    AdRequest adRequest = new AdRequest.Builder ().Build ();
    interstitital.LoadAd (adRequest);
  }

  private void ShowinterstitialAd() {
    if (interstitital.IsLoaded ()) {
      interstitital.Show ();
    }
  }
}
\$\endgroup\$
3
  • \$\begingroup\$ Where is your admob id? Or you interstitial ad id? You are just passing in an empty string. \$\endgroup\$ Feb 4 '19 at 20:00
  • 2
    \$\begingroup\$ I hid it to prevent it from being stolen. I actually added both. \$\endgroup\$ Feb 4 '19 at 20:02
  • \$\begingroup\$ Note that if your ids were stolen the only thing someone would be able to do with them is give you more money by serving ads in their app using your ids. \$\endgroup\$
    – Ed Marty
    Feb 5 '19 at 2:29
0
\$\begingroup\$

The problem is within this method

public void MenuyeDon () {
    SceneManager.LoadScene ("MainMenu");
    ShowinterstitialAd ();
}

You are loading a new scene, then showing the ad. The code below the loading scene never gets executed. What you need to do is something like this:

void Start () {
    OverSes.Play ();
    MobileAds.Initialize ("");

    // This assigns the method HandleOnAdClosed to execute when the ad is closed
    this. interstitital.OnAdClosed += HandleOnAdClosed;

    RequestinterstitialAd ();
}

public void MenuyeDon () {
    if (interstitital.IsLoaded ()) {
        interstitital.Show ();
    } else {
        SceneManager.LoadScene ("MainMenu");
    }
}

// This will be called when the ad is closed
public void HandleOnAdClosed(object sender, System.EventArgs args)
{
    SceneManager.LoadScene ("MainMenu");
}

What this will do is when the button is clicked, it will show the ad, then when the ad is closed, it loads the scene.

\$\endgroup\$
5
  • \$\begingroup\$ ı tried but not working. ı clicked "MenuyeDon" Buttons but not "MainMenu" scene open. \$\endgroup\$ Feb 4 '19 at 20:37
  • \$\begingroup\$ Are you doing this in the editor? Because the ad won't actually load unless on a real mobile device. Check the console window and you should see logs about the interstitial ad. \$\endgroup\$ Feb 4 '19 at 20:40
  • \$\begingroup\$ ı tried bluesctacks. Is that the problem? \$\endgroup\$ Feb 4 '19 at 20:50
  • \$\begingroup\$ I Don't think blue stack is a problem because it shows ads without any issue. But I would recommend using a physical device Also check logcat for any clue or Problem. \$\endgroup\$
    – Swati
    Jul 30 '20 at 5:01
  • \$\begingroup\$ To me, your posted code looks fine except that little issue @GaryHoliday mentioned. But the problem might be caused by Admob SDK or the unity integration or Android resolver. There are so many things that go wrong. So Checking LogCat is a good idea. \$\endgroup\$
    – Swati
    Jul 30 '20 at 5:08

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.