Lynnstrum Lynnstrum - 1 year ago 82
C# Question

Unity3D Show add every 5 restarts

I'm trying to make it to where when you restart the game it shows an advertisement after 5 times. This is what I have so far. This does not work. This was tested.

private int restartNum {
get {
return PlayerPrefs.GetInt ("restartNum");
set {
PlayerPrefs.SetInt ("restartNum", value);

public void OnButtonClick(string sceneName)
restartNum += 1;
if (restartNum == 5) {// adding this makes it not work
ShowAd ();// This works by itself
restartNum = 0;
SceneManager.LoadScene (sceneName);

Answer Source

if (restartNum == 5) will only restart when restartNum is 5 and this will be true only once. After that, restartNum will always be > 5 and your if statement will never be true again. To get it work every 5 times, use the modulus operator (%).

Change your

if (restartNum == 5)


if (restartNum % 5 == 0)

It will divide restartNum by 5 each time and check if the remainder is 0. If 0 then this is a 5th time.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download