Trying to Load a new banner ad crashes my Unity game

86 views
Skip to first unread message

CakeDisaster

unread,
Mar 26, 2024, 12:14:18 PM3/26/24
to Google Mobile Ads SDK Developers
  Hello, I need some help with my banner ad. It's initializing perfectly and working well. However, recently I tried to implement a feature where if the player clicks on the banner ad, I'd like it to refresh with a new request or a new ad

 Here is my BannerAdCode and the point where I trigger its instantiation event, which I can share publicly. Unfortunately, destroying the banner ad leads to an instant crash. I attempted to address this issue by unsubscribing from the events, but it didn't resolve the problem. I'm willing to share any logs or folders if it would be helpful

void Start()
{
MobileAds.Initialize(initStatus =>
{
OnSceneLoaded(SceneManager.GetActiveScene(), LoadSceneMode.Single);
SceneManager.sceneLoaded += OnSceneLoaded;
});

}

private void OnSceneLoaded(Scene scene, LoadSceneMode sceneMode)
{
AdInitialized?.Invoke();
}

AdInitialized essentially switches scenes. I initiate this script on scene index 0 and maintain it until the index 1 scene ( GoogleAdMobInitialize is set as "don't destroy on Load").

public class BannerAdManager : MonoBehaviour
{
private string adUnitId = "this is my unit id";

BannerView _bannerView;

public static Action<string> BannerAdStatus;
public static Action<bool> BannerAdLoaded;

private void Awake()
{
GoogleAdMobInitialize.AdInitialized += OnAdMobInitialized;
}

private void OnDestroy()
{
GoogleAdMobInitialize.AdInitialized -= OnAdMobInitialized;
}

private void OnAdMobInitialized()
{
LoadAdBannerAd();
}

private void LoadAdBannerAd()
{
if (_bannerView != null) return;

_bannerView = new BannerView(adUnitId, AdSize.Banner, AdPosition.Top);

var adRequest = new AdRequest();
Debug.Log("Loading banner ad.");

_bannerView.LoadAd(adRequest);

ListenToAdEvents();
}


/// <summary>
/// listen to events the banner view may raise.
/// </summary>
private void ListenToAdEvents()
{
// Raised when an ad is loaded into the banner view.
_bannerView.OnBannerAdLoaded += BannerLoadEvent;
// Raised when an ad fails to load into the banner view.
_bannerView.OnBannerAdLoadFailed += BannerAdRebuild;
// Raised when a click is recorded for an ad.
_bannerView.OnAdFullScreenContentClosed += BannerAdRebuild;
}

private void BannerLoadEvent()
{
Debug.Log("Banner view loaded an ad with response : "
+ _bannerView.GetResponseInfo());
BannerAdStatus("LOADED ");

BannerAdLoaded?.Invoke(true);
}

private void BannerAdRebuild(LoadAdError error)
{
DestroyBanner(LoadAdBannerAd);
}

private void BannerAdRebuild()
{
DestroyBanner(LoadAdBannerAd);
}

private void DestroyBanner(Action callback = null)
{
if (_bannerView != null)
{
_bannerView.OnBannerAdLoaded -= BannerLoadEvent;
_bannerView.OnBannerAdLoadFailed -= BannerAdRebuild;
_bannerView.OnAdFullScreenContentClosed -= BannerAdRebuild;

_bannerView.Destroy();
_bannerView = null;
}

callback?.Invoke();
}

Mobile Ads SDK Forum Advisor

unread,
Mar 27, 2024, 2:10:21 AM3/27/24
to cakedi...@gmail.com, google-adm...@googlegroups.com
Hi,

I will check with our team regarding your issue and one of my team members will reach out to you once we have an update on this. Meanwhile, your patience is highly appreciated.
 
This message is in relation to case "ref:!00D1U01174p.!5004Q02s0FSg:ref" (ADR-00228769)

Thanks,
 
Google Logo Mobile Ads SDK Team


Mobile Ads SDK Forum Advisor

unread,
Apr 3, 2024, 5:01:18 AM4/3/24
to cakedi...@gmail.com, google-adm...@googlegroups.com
Hi,

As per Banner ad guidance your use case might lead to policy issues. Not following these guidelines may lead to invalid activity and/or may result in Google disabling ad serving to your app. Please visit our Ad Traffic Quality Resource Center for more information.
Reply all
Reply to author
Forward
0 new messages