- Home /
How do I delay an Ad?
So I have have a script that plays an add every 90 seconds. Every time you change scene (which is quite regular) An ad pops up.
Does anyone know how to delay this
Script:
using UnityEngine;
using System.Collections;
using UnityEngine.Advertisements;
public class ShowAds : MonoBehaviour
{
float timer;
void Start()
{
Advertisement.Initialize ("<YOUR GAME ID HERE>"); //REMEMBER TO EDIT!!
}
void Update()
{
timer -= Time.deltaTime;
ShowAd ();
}
public void ShowAd()
{
if (Advertisement.IsReady () && timer <= 0)
{
Advertisement.Show ();
timer = 90.0f;
}
}
}
Basically every time you change scene a video ad pops up straight away. this would be quite annoying and I want to make it so It would be delayed by about a $$anonymous$$ute or so.
also to what you said, where do I put or replace the code? (I don't know c#)
Answer by Salmjak · Mar 12, 2016 at 08:41 PM
@chesterhilly You can use Time.time % 90 == 0
as the condition. Using Time.time (notice "...since the start of the game" so it shouldn't be affected by scene changes) and the modulo operator.
My example will make it so the ad is displayed every 90 seconds, regardless of scene change (sine Time.time should be "since the start of the game" and not since the start of the scene, there will be no reset.
If you definitely don't want to show an Ad in the beginning of a scene you can use another check Time.timeSinceLevelLoad > 60f
(time.timeSinceLevelLoad) this would only show the ad if 60 seconds or more have passed since the scene was loaded.
Now just combine these examples into one if-statement :)
The final code would look something like:
public void ShowAd()
{
if (Time.time % 90 == 0 && Time.timeSinceLevelLoad > 60) //Is the time divisible by 90 with no remainder? Has atleast 60 seconds passed since the scene was loaded?
{
Advertisement.Show ();
}
}
@chesterhilly Please mark answer as correct if it works :)
It should be noted that Time.time % 90f == 0
will only be true when it's exactly 0. So if the function is called when Time.time = 90.1 it won't be true. You might go around this by adding a range ins$$anonymous$$d. Such as Time.time % 90f <= 0.01f
. But this will ins$$anonymous$$d be true several frames in a row. Oh, btw! Don't forget to also put Advertisement.IsReady()
in there! So the code would look like this:
public void ShowAd()
{
if (Advertisement.IsReady() && Time.time % 90f <= 0.01f && Time.timeSinceLevelLoad > 60) //Is the time divisible by 90 with no remainder? Has atleast 60 seconds passed since the scene was loaded?
{
Advertisement.Show ();
}
}
You might have to add even more checks in there to make sure the an ad is only shown once every 90 second, no more no less.
Answer by aditya · Mar 13, 2016 at 07:24 AM
you can simply use Invoke("ShowAd", 5f), this will show th ad after 5 seconds (or change it on your needs)
Your answer
Follow this Question
Related Questions
Adding a delay for weapon switching 1 Answer
Unity Ads Banner not working. 0 Answers
Admob script help 0 Answers
Non Spammable Skill/Button 1 Answer
Function delay? 1 Answer