How to make UnityAds appear after 3 deaths?

Viewed 59

Right now the game shows ads each and every time the player dies. I want the ads to appear after the third death of the player. Please guide me as I am a beginner with no real knowledge of coding. Please also specify if I need to attach the script to a specific game object.

I have put the ads code below. Thanks.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;

public class GameManager : MonoBehaviour

{
    public GameObject gameOverCanvas;

public GameObject Interstitial;

public int DeathCount = 0;

private void Start()
{
    Time.timeScale = 1;
   
}


public void GameOver()
{
    gameOverCanvas.SetActive(true);
    Time.timeScale = 0;
    
    DeathCount++;
    
}

private bool DeathCountCheck()
{


    if (DeathCount == 3)
    {
        // Reset the variable, otherwise it will increase indefinitely and the condition will not be met in the future
        Interstitial.GetComponent<InterstitialAds>().ShowInterstitial();
        DeathCount = 0;
        return true;

    }

    return false;

}

public void Replay()
{
    SceneManager.LoadScene(0);
}
1 Answers

if "deathCount" increases each time the player dies, then when the ad is meant to show you just check that variable and reset it appropriately.

// Function that checks if there were 3 deaths
private bool DeathCountCheck()
        {

            
            if (deathCount == 3)
            {
                // Reset the variable, otherwise it will increase indefinitely and the condition will not be met in the future
                deathCount = 0;
                return true;
            }

            return false;

        }

Then in the "ShowInterstitialAd" just add another condition

    public void ShowInterstitial()
{
    if (Advertisement.IsReady(interstitialID) && DeathCountCheck())
    {
        Advertisement.Show(interstitialID);
    }
}
Related