PlayerPrefs and Unity Ads combined? Pls help me guys, i am beginner!

Hello everyone, i have a problem with Unity Ads.
I want to Implement Ads every 5-10 times (dont know yet) Ads when the Player tips on the “Play Again” Button.

My Problem now is, Unity is loading the Ads everytime i Push dat Button, Can you help me please?

This is my code:

using System.Collections;
using UnityEngine.Advertisements;
using System.Collections.Generic;
using UnityEngine;

public class PlayAd : MonoBehaviour {

    public static int counter = 0;

    public void Start()
    {

    }

    public void ShowAd()    {

        Advertisement.Show();

    }


    public void CountAds()    {

        counter = PlayerPrefs.GetInt("AdCounter", counter);

        PlayerPrefs.SetInt("AdCounter", counter++);
        PlayerPrefs.Save();

        if ((PlayerPrefs.GetInt("AdCounter") <= 3))

        {
        PlayerPrefs.DeleteAll();
        ShowAd();

        }
    }
}

Thank you in Advance guys!

You’re saying : If PlayerPrefs.GetInt (“AdCounter”) is LESS THAN or equal to 3, show ad

oh u are right i did this for testing if the if statement works but there is still another mistake in it, now the ad doenst appear anymore

I think you should probably just delete the add counter function.
Inside Start(), read the adCounter variable and store it.

Now, inside ShowAd(), you can add 1 to the counter (for every play). When it equals 5/10 , then show the ad , plus set the adCounter to zero and save it.

I think that will cover everything that was in the other function. I’m also thinking that deleting all of your player prefs for that one variable could be a huge nuissance/undesired, if you ever store anything else in there, it would be deleted! :slight_smile:

ok ok, tried like u say, now it works, while trying it shows the ad after 3 times playing (just for the try) BUT from then it shows the ad every time, so there is something wrong with reseting the Counter, can u give me one more little hint? :smile:

public class PlayAd : MonoBehaviour {

    public static int adCounter = 0;

    public void Start()
    {
        PlayerPrefs.SetInt("AdCounter", adCounter);
        PlayerPrefs.Save();
    }

    public void ShowAd()    {

        PlayerPrefs.GetInt("AdCounter", adCounter++);

        if (adCounter>= 3) {
            PlayerPrefs.SetInt("AdCounter", 0);
            PlayerPrefs.Save();
            Advertisement.Show();
        }
    }

}

Just remove all of playerprefs. Static variables are persistent through scene changes, meaning you can use your adCounter variable without the player prefs.

public class PlayAd : MonoBehaviour
{
    static int adCounter;

    public void ShowAd ()
    {
        adCounter++;
        if (adCounter == 3)
        {
            adCounter = 0;
            Advertisement.Show();
        }
    }
}

Just to double check this is what you want… do you want this to save between sessions, also? because if you do, you want to getint at the start, not save… if you don’t want it to save, you don’t need to get or set it :slight_smile:

Okay, so past that… I would modify your code slightly:

public void ShowAd()    {
   adCounter++; // we already have this value, no need to get it, again :)

        if (adCounter>= 3) {
            PlayerPrefs.SetInt("AdCounter", 0);
            PlayerPrefs.Save();
            Advertisement.Show();
        }
    }

That is very true, and a good suggestion, if he doesn’t want to save the ‘adCounter’ between play sessions :slight_smile:

  • I was getting to that :wink:

thanks to both of you, it works without the playerprefs like Antonie said, have a great day! and really THANKS FOR THE FAAAAST help :slight_smile:

Cheers :slight_smile: