- Home /
Show an ad once every 2 times
An advertisement from admob shows in my android app every time the user loses. However I want it to appear after the loss once every 2 loses. E.g i died and the ad showed the next time i died it didnt show after that it showed and so on. It takes a gap and then shows again after.
//Setting up the ad
InterstitialAd interstitial;
private void RequestInterstitial()
{
#if UNITY_ANDROID
string adUnitId = "ca-app-pub-5920324855307233/4763839506";
#elif UNITY_IPHONE
string adUnitId = "ca-app-pub-5920324855307233/4763839506";
#else
string adUnitId = "unexpected_platform";
#endif
// Initialize an InterstitialAd.
interstitial = new InterstitialAd(adUnitId);
// Create an empty ad request.
AdRequest request = new AdRequest.Builder().Build();
// Load the interstitial with the request.
interstitial.LoadAd(request);
}
void Start(){
RequestInterstitial();
}
void Update(){
if (interstitial.IsLoaded()) {
interstitial.Show();
}
}
Answer by pankajb · May 10, 2016 at 08:37 PM
Something like this should solve your issue
void Start(){
PlayerPrefs.SetInt("ShouldShowAd", 0);
}
void ShowAd(){
if(PlayerPrefs.GetInt("ShouldShowAd") == 0)
{
PlayerPrefs.SetInt("ShouldShowAd", 1);
// do nothing and don't show ad
}
else
{
PlayerPrefs.SetInt("ShouldShowAd", 0);
// show ad here
}
}
// call show ad upon player death
@pankajb this is absolutely the standard way to go about it, and it even has the added benefit that it saves whether or not it is time for an ad between play sessions. There is, however, one flaw to consider; PlayerPrefs aren't secure at all. At. All. Particularly on Android. PlayerPref variables are essentially stored in a text file that anyone with the right tools could go in and edit, and it isn't incredibly hard to find those tools, either. If they wanted, they could potentially edit the PlayerPref variable to never see an ad in your game, and although it isn't common by any means, it does happen. If this doesn't bother you, or you're confident in your own form of d.i.y. PlayerPref decryption, then by all means, go for it. It is an awesomely simple and effective way to do this. I just want you to be aware of the drawbacks.
Whilst this is potentially true, they would have to edit the playerprefs after every death to avoid seeing ads. It would be less hassle just to close the ad when it shows.
Also, your post was a comment and not an answer so you should convert it to a comment.
I'm looking at the broader aspect of this answer. It would be annoying and tedious to do so, but some people don't care. Plus, you have to think about people who see this answer and say something like "i'm gonna use this answer, but i'll do it once every 4 games" or something like that. Then it would be a lot more likely. Or if the person who asked the question uses this, says "PlayerPrefs are great, i'm also gonna implement them here and here as well" not knowing what he's getting into. You have to be aware of how much reach an answer on this site has
Also, I clicked reply, but it still posted as an answer. I'll try to convert it
Answer by Munchy2007 · May 10, 2016 at 11:08 PM
My suggestion would be to do it like this (the method I use myself).
Player dies
Check interstitial is ready
If it is then show it (but don't request another)
If it isn't then request an interstitial (but don't show it.)
Rinse and repeat
This results in an interstitial being shown every two deaths, with no need for counters, playerprefs saving etc. etc.
Your answer
![](https://koobas.hobune.stream/wayback/20220612080534im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Changing Booleans Throught Certain Distance 1 Answer
I am trying to make a movement script for my main camera but it isnt working! 2 Answers
How to store a boolean for each variable in a class 1 Answer
Setting boundary for Z axis 1 Answer
How do I do a double Floating joystick? (Direction and movement (one for each))? 0 Answers