• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
Question by kriyor · Jan 25, 2018 at 05:42 PM · unity 5unity 2dunityeditorunity ads

How to make script remember a counter and display an ad every 5th time?

I am finishing up a project for my university and I need to add a monetization method. I stopped at Interstitials from AdMob that will be displayed whenever the GameOver screen appears. My problem is that I want them to appear on every fifth appearance of this GameOver screen and so far I haven't managed to make anything work. The interstitial appears ever time. Another problem that may affect this is that the script restarts every time I press Restart, so the ad counter won't really work. Here is my code and I hope someone can assist!

Thank you!

 public class GameOver: MonoBehaviour {
 
     static int loadCount = 0;
 
     private InterstitialAd interstitial;
     public GameObject gameOverScreen;
 
 
     bool gameOver;
 
     // Use this for initialization
     void Start () {
         FindObjectOfType<PlayerController>().OnPlayerDeath += OnGameOver;
         this.RequestInterstitial();
 
     }
 
     // Update is called once per frame
     public void Update () 
         if (gameOver)
         {
             if (Input.GetKeyDown (KeyCode.Space))
             {
                 SceneManager.LoadScene(1);
             }
         }
     }
 
     private void RequestInterstitial()
 
     {
 #if UNITY_ANDROID
         string adUnitId = "ca-app-pub-3940256099942544/1033173712";
 #elif UNITY_IPHONE
         string adUnitId = "ca-app-pub-3940256099942544/4411468910";
 #else
         string adUnitId = "unexpected_platform";
 #endif
 
         // Initialize an InterstitialAd.
         interstitial = new InterstitialAd(adUnitId);
 
         AdRequest request = new AdRequest.Builder().Build();
         // Load the interstitial with the request.
         interstitial.LoadAd(request);
     }
 
     private void AdDisplay()
     {
         if (interstitial.IsLoaded())
         {
             interstitial.Show();
         }
     }
 
     void OnGameOver()
     {
 
         gameOverScreen.SetActive (true);
         kmHighscore.text = GetScore().ToString("F0");
 
         gameOver = true;
         if (loadCount % 5 == 0) 
         {
             AdDisplay();
         }
         loadCount++;
     }
 }
Comment

People who like this

0 Show 0
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image

Answer by vanjasretenovic123 · Feb 05, 2018 at 08:10 PM

Try to save float with


PlayerPrefs.SetFloat("name of variable", value of variable); Eg. ("loadCount",5); or ("loadCount", loadCount) ; PlayerPrefs.Save();


And get float with


float newcount= PlayerPrefs.GetFloat("loadCount",newcount);


And then compare value of newcount


if(newcount == 5) { //loadAD }

Comment

People who like this

0 Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image vanjasretenovic123 · Feb 05, 2018 at 08:14 PM 0
Share

I need your help too. I made script for show rewarded video admob, but ads are loaded, but not showing.


Here is my code:


using System.Collections; using System.Collections.Generic; using UnityEngine; using GoogleMobileAds; using GoogleMobileAds.Api;

public class GoogleAdMobADS : MonoBehaviour { public bool pr; // Use this for initialization private RewardBasedVideoAd rewardBasedVideo;

 public void Start()
 {
     pr = false;
     // Get singleton reward based video ad reference.
     //this.rewardBasedVideo = RewardBasedVideoAd.Instance;
     // Get singleton reward based video ad reference.
     this.rewardBasedVideo = RewardBasedVideoAd.Instance;
     
     this.RequestRewardedVideo();
     
 }

 // Update is called once per frame
 void Update () {
     if (rewardBasedVideo.IsLoaded())
     {
         pr=true;
         rewardBasedVideo.Show();
     }

 }
 private void RequestRewardedVideo()
 {
     //ca-app-pub-3940256099942544/5224354917
     string adUnitId = "ca-app-pub-3940256099942544/5224354917";
     // Create an empty ad request.
     AdRequest request = new AdRequest.Builder().Build();
     // Load the rewarded video ad with the request.
     rewardBasedVideo.LoadAd(request, adUnitId);
 }

}


avatar image vanjasretenovic123 · Feb 05, 2018 at 08:15 PM 0
Share

Or you can random show ads


float a=Random.Range(0,5); if(a==3) { //show AD }

avatar image markknoffer vanjasretenovic123 · Mar 09, 2021 at 10:05 AM 0
Share

hi, thanks a lot for this.

i am trying to show a adcall every 3 times randomly so that the ad does not get called every time when the game level starts.

is this code you posted a way to implement that?

float a=Random.Range(0,5); if(a==3) { //show AD }

i would try to post my ad call instead of //show AD

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

200 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Does Unity rasterize .psb files when uploaded? 0 Answers

Diagonal movement, stopping at screen edge 0 Answers

How to make my game stop once the timer meets zero in unity. 1 Answer

How to Setactive(true) the other class ? 0 Answers

How to detect touchscreen swipe more optimally? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges