I have gotten my Ads working in my game using the following script ( which is attached to the camera in my main menu)
using UnityEngine;
using System.Collections;
using GoogleMobileAds.Api;
public class Request2 : MonoBehaviour
{
// Use this for initialization
void Start()
{
RequestBanner();
}
// Update is called once per frame
void Update()
{
}
private void RequestBanner()
{
#if UNITY_EDITOR
string adUnitId = "unused";
#elif UNITY_ANDROID
string adUnitId = "///";
#elif UNITY_IPHONE
string adUnitId = "INSERT_IOS_BANNER_AD_UNIT_ID_HERE";
#else
string adUnitId = "unexpected_platform";
#endif
BannerView bannerView = new BannerView(adUnitId, AdSize.Banner, AdPosition.Top);
AdRequest request = new AdRequest.Builder().Build();
bannerView.LoadAd(request);
}
}
It works fine. However, I want the ads to disappear when I get into my actual gameplay. It can appear on any other level just NOT the gameplay. I’ve tried seemingly every solution , and the ads still show up every scene. So , how can I make my ads show up on all levels except my gameplay?
Still doesn’t work. I’ve tried this before , and I think it doesn’t work because the Application class is now obsolete. So , what is the equivalent in the SceneManager class?
using UnityEngine;
using System.Collections;
using GoogleMobileAds.Api;
using UnityEngine.SceneManagement;
public class Request : MonoBehaviour
{
// Use this for initialization
void Start()
{
RequestBanner();
}
// Update is called once per frame
void Update()
{
}
private void RequestBanner()
{
#if UNITY_EDITOR
string adUnitId = "unused";
#elif UNITY_ANDROID
string adUnitId = "111;
#elif UNITY_IPHONE
string adUnitId = "INSERT_IOS_BANNER_AD_UNIT_ID_HERE";
#else
string adUnitId = "unexpected_platform";
#endif
BannerView bannerView = new BannerView(adUnitId, AdSize.Banner,AdPosition.Bottom);
AdRequest request = new AdRequest.Builder().Build();
bannerView.LoadAd(request);
if (Application.loadedLevel == 1)
{
bannerView.Hide();
}
}
}
Did you try to build the project for Android or iOS and try to run it? I know many of Google’s plugins don’t work in the Unity editor. Perhaps try putting the Debug.Log before banerview.Destroy() to see if it’s getting called at all. If it isn’t then your loaded level isn’t 1. Or you can try to add this to see if the if statement is true:
Alright , the banner destroyed method works. It destroys on level 0 (which is not what I want) However, it keeps telling me the loaded level is 0. Then when I click on the start button ( which leads to scene 1) it still says the current level is 0. Hmm.
EDIT: I built to Android , and the ad takes like 15 seconds to show up ( is there anyway to fix that?)
, and when I go to my actual game , the ad still appears , but is glitchy. I’m not sure if that means Unity is TRYING to delete the ad, but just can’t.
I think I’ve found it out. I attached the following script to my second scene ( the one I want the ad to disappear on) , but I get this error :
NullReferenceException: Object reference not set to an instance of an object
TestLevel.Start () (at Assets/Scripts/TestLevel.cs:10)
Here’s the script :
using UnityEngine;
using System.Collections;
public class TestLevel : MonoBehaviour {
public Request req; // Reference to my script that generates the banner ad
// Use this for initialization
void Start () {
if (Application.loadedLevel == 1)
{
req.bannerView.Destroy(); // This is the script that causes the error
Debug.Log(" Banner Destroyed");
}
}
// Update is called once per frame
void Update () {
}
}
Other script ( The one you saw before)
using UnityEngine;
using System.Collections;
using GoogleMobileAds.Api;
using UnityEngine.SceneManagement;
public class Request : MonoBehaviour
{
public BannerView bannerView;
// Use this for initialization
public void Start()
{
#if UNITY_EDITOR
string adUnitId = "11";
#elif UNITY_ANDROID
string adUnitId = "11";
#elif UNITY_IPHONE
string adUnitId = "INSERT_IOS_BANNER_AD_UNIT_ID_HERE";
#else
string adUnitId = "unexpected_platform";
#endif
BannerView bannerView = new BannerView(adUnitId, AdSize.Banner, AdPosition.Bottom);
AdRequest request = new AdRequest.Builder().Build();
bannerView.LoadAd(request);
}
// Update is called once per frame
void Update()
{
}
}
I think this would work if the error didn’t happen. How do I fix it?
using UnityEngine;
using System.Collections;
public class TestLevel : MonoBehaviour {
public Request req; // Reference to my script that generates the banner ad
// Use this for initialization
void Start () {
if (Application.loadedLevel == 1)
{//error here
req.bannerView.Destroy();
Debug.Log(" Banner Destroyed");
}
}
// Update is called once per frame
void Update () {
}
}
I clicked on the error and now it’s telling me it occurs on the bracket below the if function.
I tried , and I get this error.
NullReferenceException: Object reference not set to an instance of an object
Request.Start () (at Assets/Scripts/Request.cs:25)
using System.Collections;
using UnityEngine;
using GoogleMobileAds.Api;
using UnityEngine.SceneManagement;
public class Request : MonoBehaviour
{
public BannerView bannerView;
public Request req;
public void Start()
{
#if UNITY_EDITOR
string adUnitId = "ca-app-pub-7445639135429800/2022704376";
#elif UNITY_ANDROID
string adUnitId = "ca-app-pub-7445639135429800/2022704376";
#elif UNITY_IPHONE
string adUnitId = "INSERT_IOS_BANNER_AD_UNIT_ID_HERE";
#else
string adUnitId = "unexpected_platform";
#endif
BannerView bannerView = new BannerView(adUnitId, AdSize.Banner, AdPosition.Bottom);
AdRequest request = new AdRequest.Builder().Build();
bannerView.LoadAd(request);
req.bannerView.Destroy();
}
// Update is called once per frame
void Update()
{
}
}
And this one
using UnityEngine;
using System.Collections;
public class TestLevel : MonoBehaviour {
public Request req;
// Use this for initialization
void Start () {
if (Application.loadedLevel == 1)
{//error here
req .bannerView.Destroy();
Debug.Log(" Banner Destroyed");
}
}
// Update is called once per frame
void Update () {
}
}
I dragged the GameObject into the slots for both scenes , and I still get the error.
Are you saying that in TestLevel , I should call the OnDestroy Method?
Here’s the updated script.
using UnityEngine;
using System.Collections;
public class TestLevel : MonoBehaviour
{
public Request req;
// Use this for initialization
void Start()
{
OnDestroy();
}
// Update is called once per frame
void Update()
{
}
public void OnDestroy()
{
if (Application.loadedLevel == 1)
{//error here
req.bannerView.Destroy();
Debug.Log(" Banner Destroyed");
}
}
}
Yeah , I still get the error. I think I’m gonna give up on Admob, Is there any other ad network that provides banner ads and supports Unity (and that isn’t as frustrating to set up as Admob)
I’d still ask on the GitHub Admob page, just in case you could get it to work. I wish I could talk from personal experience, but I haven’t shipped a project yet (however I’m working on something major at the moment ;)). Seems like Millennial Media, Mopub and AirPush are good other options:
I used Admob before just to try it out, however I never used the Banner.Destroy() method. Overall I think Google products are pretty low quality when it comes to the developer side of things. Stuff like Google, Gmail, Drive, YouTube is OK but developing for Android and using their plugins is a convoluted mess when compared to other solutions.
I just asked (on the github page). Hopefully I can get an answer. I’ll def try those out if all else fails with Admob. Thanks for all the help man!
I’ll post the solution if I get it!