How to show Ad every so often?

Ok so I have managed to integrate ads into my project but I don’t have alot of experience with c#.

So I just wanted to know if anyone would know how to change a script that creates an button (to show an ad) so the ad would appear every amount of time set. e.g 5 minutes.

Here is the script example:

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

public class ShowAds : MonoBehaviour
{
    void OnGUI ()
    {
        Rect buttonRect = new Rect (10, 10, 150, 50);
        string buttonText = Advertisement.IsReady () ? "Show Ad" : "Waiting...";

        if (GUI.Button (buttonRect, buttonText)) {
            Advertisement.Show ();
        }
    }
}

Not sure your users will necessarily watch an ad, just because a button is shown in your game :wink: See e.g. Unity Blog for suggestions on how to integrate ads into a game.

Anyways, using a variable to keep track of when an ad was last shown, could be used to achieve what you ask for. Example code (have not tested, even tried to compile it though):

    using UnityEngine;
    using System.Collections;
    using UnityEngine.Advertisements;
    
    public class ShowAds : MonoBehaviour
    {
        private float timeLastAdWasShown;

        void OnGUI ()
        {
            if (Time.time - timeLastAdWasShown < 5 * 60)  // in seconds
                return;

            Rect buttonRect = new Rect (10, 10, 150, 50);
            string buttonText = Advertisement.IsReady () ? "Show Ad" : "Waiting...";
    
            if (GUI.Button (buttonRect, buttonText)) {
                timeLastAdWasShown = Time.time;
                Advertisement.Show ();
            }
        }
    }

And don’t forget every user has a number limit of watching ads every day.
https://unityads.unity3d.com/help/faq/monetization#why-am-i-only-seeing-2-or-3-ads-at-a-time-