Search code examples
c#unity-game-enginein-app-purchaseunityads

Unity IAP not working as expected to remove ad banner


I am trying to implement remove ads feature for a unity mobile app, I didn't integrate the IAP service with google play yet, but I am just trying to figure out the logic and make it work in a test environment first. I successfully displayed a bottom screen ad banner and when user is logged in, on his profile screen there is a button to remove ads, OnClick a user attribute of name "subscribed" in the database is set to true and I remove the ad button and hide ad banner, So on the next login I implemented a function to check user subscription and therefore if user is subscribed then the remove ads button and the ad banner are not displayed. This Coroutine is run on user login and on subscribed users login the remove ads button is not displayed, but the ad banner is still on and i get the NullReferenceException error on the line where i call the HideBanner function for the AdDisplay instance.

I am using Unity Ads to display the ad banner.

private IEnumerator CheckSubscription()
{
    var DBTask = DBreference.Child("users").Child(User.UserId).GetValueAsync();
    

    yield return new WaitUntil(predicate: () => DBTask.IsCompleted);

    if(DBTask.Exception != null)
    {
        Debug.LogWarning(message: $"Failed to retrieve user subscirption status");
    }
    else
    {
        DataSnapshot snapshot = DBTask.Result;
        var isSubscribed = snapshot.Child("subscription").Value.ToString();
        if(isSubscribed == "true")
        {
            UIManager.instance.RemoveAds();
            AdDisplay.instance.showAds = false;  //Console error on this line of code
        }
        else
        {
            AdDisplay.instance.showAds = true;
        }
    }
}


Solution

  • I managed to fix it using

    Advertisement.Banner.Hide()

    and setting a flag that checks if user owns remove ads feature and if

    flag == true

    The hideBanner method executes, else ads just run normally.