Unity ads Never End. They Loop

Hello my unity ads are working but don't stop when I click the close button it just loads another ad. How could I make it so it will stop playing after one ad and proceed with loading the menu?

I am VERY new to C# FYI.

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

//GameOver page when the plane is destroyed
public class GameOverPage : MonoBehaviour {

public GUISkin skin; //skin for button styles
public static int start; //static integer indicates to show or hide Start/GameOver page
public static bool running; //static variable indicates if the plane is destroyed or not


void OnGUI(){
            GUI.skin = skin;

            //if start is not equal to 0 and running is false then show GameOver page buttons
            if ((StartPage.start != 0) && !PlaneMovement.running) {
        if (Advertisement.IsReady ()) {
            Advertisement.Show ();

        }



        if (GUI.Button (new Rect (Screen.width / 2.378f, Screen.height / 1.34f, Screen.width / 6f, Screen.height / 10.10f), "", skin.GetStyle ("Restart"))) {  
                            Application.LoadLevel (1);  
                            PlaneMovement.running = true;
                    }
        if (GUI.Button (new Rect (Screen.width / 1.50f, Screen.height / 1.34f, Screen.width / 6.0f, Screen.height / 10.1f), "", skin.GetStyle ("Home"))) {
                            Application.LoadLevel (0);  
                            PlaneMovement.running = false;
                            StartPage.start = 0;
                    }
        if (GUI.Button (new Rect (Screen.width / 5.7f, Screen.height / 1.34f, Screen.width / 6f, Screen.height / 10.10f), "", skin.GetStyle ("Website"))) {
                            Application.OpenURL ("http://www.skyboxertech.weebly.com");
                    }


        }
    }
}

EDIT: This is what i've got. Im not sure how to implement the timer it shows all the ads again in a loop and is not limiting them to every 5th time.

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

//GameOver page when the plane is destroyed
using UnityEngine.SceneManagement;


public class GameOverPage : MonoBehaviour {

public GUISkin skin; //skin for button styles
public static int start; //static integer indicates to show or hide
bool showAd = true; 
public static bool running; //static variable indicates if the plane is destroyed or not
public int gameOverCounter = 0; 

void OnGUI(){
    GUI.skin = skin;

    //if start is not equal to 0 and running is false then show GameOver page buttons
    if ((StartPage.start != 0) && !PlaneMovement.running) {
        gameOverCounter++;

        if (gameOverCounter >= 5) {
            showAd = true;
        if(showAd){ //Check if we should show add 
            if (Advertisement.IsReady ()) {
                Advertisement.Show ();
                showAd = false; 
                    gameOverCounter = 0;
                }
            }
        }

        if (GUI.Button (new Rect (Screen.width / 2.378f, Screen.height / 1.34f, Screen.width / 6f, Screen.height / 10.10f), "", skin.GetStyle ("Restart"))) {  
            SceneManager.LoadScene (1);  

            PlaneMovement.running = true;
        }
        if (GUI.Button (new Rect (Screen.width / 1.50f, Screen.height / 1.34f, Screen.width / 6.0f, Screen.height / 10.1f), "", skin.GetStyle ("Home"))) {
            SceneManager.LoadScene (0);  
            PlaneMovement.running = false;
            StartPage.start = 0;
        }
        if (GUI.Button (new Rect (Screen.width / 5.7f, Screen.height / 1.34f, Screen.width / 6f, Screen.height / 10.10f), "", skin.GetStyle ("Website"))) {
            Application.OpenURL ("http://www.skyboxertech.weebly.com");
        }


    }
}
}

Answers


The OnGUI() function is called several times per frame. As long as StartPage.start is not 0 and PlaneMovement.running is not true your add will always show. Those two conditions are met. That's why the ads are showing. I don't know what those variables are and what you use them for but its a good idea to display your ad every 5 time the game is over instead of each time the game is opened. It is annoying and will make players delete your game.

You can create a boolean variable called showAdd that determines when to display ad. The code below will show ad once.

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

//GameOver page when the plane is destroyed
public class GameOverPage : MonoBehaviour {

public GUISkin skin; //skin for button styles
public static int start; //static integer indicates to show or hide
bool showAd = true; //Will show ad the first time
public static bool running; //static variable indicates if the plane is destroyed or not


void OnGUI(){
            GUI.skin = skin;

            //if start is not equal to 0 and running is false then show GameOver page buttons
        if ((StartPage.start != 0) && !PlaneMovement.running) {
          if(showAd){ //Check if we should show add           
            if (Advertisement.IsReady ()) {
            Advertisement.Show ();
            showAd = false; //Set to false So that we dont show ad again
          }
      }


        if (GUI.Button (new Rect (Screen.width / 2.378f, Screen.height / 1.34f, Screen.width / 6f, Screen.height / 10.10f), "", skin.GetStyle ("Restart"))) {  
                            Application.LoadLevel (1);  
                            PlaneMovement.running = true;
                    }
        if (GUI.Button (new Rect (Screen.width / 1.50f, Screen.height / 1.34f, Screen.width / 6.0f, Screen.height / 10.1f), "", skin.GetStyle ("Home"))) {
                            Application.LoadLevel (0);  
                            PlaneMovement.running = false;
                            StartPage.start = 0;
                    }
        if (GUI.Button (new Rect (Screen.width / 5.7f, Screen.height / 1.34f, Screen.width / 6f, Screen.height / 10.10f), "", skin.GetStyle ("Website"))) {
                            Application.OpenURL ("http://www.skyboxertech.weebly.com");
                    }


        }
    }
}

You can then have a integer variable that counts how many times game has been over. if it reaches 5, set showAdd to true and ad will show again.


Need Your Help

Create Dynamic Pivot - multiple rows into fields based on 2 common fields

sql oracle oracle11g

How can I pivot the PONT_LOC field into separate fields for each value, thus creating one row per SECTION & SURVEY_DATE combination, the number of rows will always very based on SECTION &

Converting DateTime to string before sending to the database

c# sql datetime

I have a method that have two DateTime and a int as its parameter e.g: