2
\$\begingroup\$

I'm building a game using unity 3d and I want to add an interstitial ads to it, but I want it to appear after the player lost every 3 times. So when the player loses for the first time the ads will appear, and when he loses for the second and third time, nothing appears until he lost for fourth time.

\$\endgroup\$
  • \$\begingroup\$ I have tried create a variable called "Count" an increment it inside the function that load lose scene, then store this variable with Playerprefs.SetInt(). It's working when lose scene is loaded for the first time because I debug the "count" variable and it shows count = 1. But when i replay the game it seems that "count" is initialize again to 0. and if I lose, count =1 and so on. \$\endgroup\$ – Boylamn Oct 31 '15 at 21:13
3
\$\begingroup\$

A static integer should do the work. You can simply increase it's value every time you load the scene.

\$\endgroup\$
  • 1
    \$\begingroup\$ No static class needed really. A static member will do \$\endgroup\$ – Igor S. Oct 31 '15 at 22:54
0
\$\begingroup\$

You could make a global variable:

Public int GameOverCount = 0

Then add 1 every time you call game over:

GameOverCount = GamoOverCount  + 1

And finally; when you start over for the 3rd, 6th, 9th..etc. time, you can use modulo(%) to check if GameOverCount is a multiple of three:

if ( (GameOverCount % 3) == 0)
{
    ShowAds()
}
\$\endgroup\$
  • \$\begingroup\$ This is what I did, but it seems not working ! \$\endgroup\$ – Boylamn Oct 31 '15 at 21:28
  • \$\begingroup\$ @Boy Try printing out GamOverCount to console after a game over and after you increment it. You could aslo do the same before you check if the GameOverCount is a multiple of three. \$\endgroup\$ – ZEKE Oct 31 '15 at 21:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.