Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

Show Admob Interstitials Every 3 Times

Discussion in 'Scripting' started by androidkafkaskafkas, Aug 2, 2018.

  1. androidkafkaskafkas

    androidkafkaskafkas

    Joined:
    Jun 23, 2018
    Posts:
    16
    Hello everyone,

    I am almost at the edge of finishing my first and simple unity game. However, i cannot handle the loading admob interstitials every 3 or 4 times of the screen load. Here is the code below.

    There are not any crashes however there are not any test ads too. I would be grateful if you could have a look at.

    Thank you.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using GoogleMobileAds.Api;
    4.  
    5. public class AdScript : MonoBehaviour
    6. {
    7.     InterstitialAd interstitial;
    8.     public string BannerId;
    9.     public string InterstitialId;
    10.  
    11.     static int loadCount = 0;
    12.  
    13.     // Use this for initialization
    14.     void Start()
    15.     {
    16.         if (loadCount % 3 == 0)
    17.         {
    18.  
    19.             RequestInterstitial();
    20.         }
    21.         loadCount++;
    22.     }
    23.  
    24.  
    25.     private void RequestInterstitial()
    26.         {
    27.  
    28.             string adUnitId = InterstitialId;
    29.  
    30.  
    31.             // Initialize an InterstitialAd.
    32.             interstitial = new InterstitialAd(adUnitId);
    33.             // Create an empty ad request.
    34.             AdRequest request = new AdRequest.Builder().Build();
    35.             // Load the interstitial with the request.
    36.             interstitial.LoadAd(request);
    37.         }
    38.  
    39.         public void showInterstitialAd()
    40.         {
    41.             //Show Ad
    42.             if (interstitial.IsLoaded())
    43.             {
    44.                 interstitial.Show();
    45.             }
    46.  
    47.         }
    48.     }
    49.  
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,599
    You are not calling showInterstitialAd() as far as I can see.
     
    androidkafkaskafkas likes this.
  3. androidkafkaskafkas

    androidkafkaskafkas

    Joined:
    Jun 23, 2018
    Posts:
    16
    Thank you for your reply.

    Erm, actually the code is working except loadCount feature when i attach to a button.

    I would be grateful if you could help me how to show ads every 3 or 4 times user clicks a button ?

    Thank you.
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,599
    Before anyone can truly answer that, could you explain how showInterstitialAd() gets called? Or is it completely not called?

    If the interstitial.LoadAd() method is triggering it showing, one way it might show every time through this scene is if you have three copies of the AdScript in your scene. Otherwise, the modulo logic in Start() should be firing just fine.
     
  5. androidkafkaskafkas

    androidkafkaskafkas

    Joined:
    Jun 23, 2018
    Posts:
    16
    i have pretty copied from step by step guide - google.

    created a game object.
    attached script to that object.
    created a restart button.
    implemented this object on that button with onclick-runtime only etc.

    now every time user clicks restart button, interstitial ads show itself. but of course as you might know, it is very annoying.

    i dont know how to count how many times users press the restart button and for example show the ads every 3 or 4 times if user press the button.

    thank you.
     
  6. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,599
    The key is you're going to have to figure out what is calling showInterstitialAd().

    It's NOT in the code you posted above. Until you solve that, you're not gonna solve this problem.

    Is it the actual BUTTON calling that function?

    If so, then move your modulo check in the Start() function into the showInterstitialAd() function, but only IF you understand that is what is happening.
     
  7. androidkafkaskafkas

    androidkafkaskafkas

    Joined:
    Jun 23, 2018
    Posts:
    16
    i have simply used this one.

     
  8. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,599
    And.... how does it connect?