Search code examples
c#unity-game-engineadmobtimedelay

Add Delay Time Admob Interstitial on Gameover


I am new in C#, anyone can help me solving this. I am creating game in Unity and have integrated admob (interstitial) in Gameover state. Now it works well, however it looks much annoying for showing insterstitial ads everytime user reach the gameover screen. So here I think it's good to add some delay time within the ads showing. As far I got some simple logic but it doesn't work as I need, not sure.

Here is my code, for example I want it to have 2 minutes delay, so at gameover it won't execute the interstitial ads if it doesn't reach the delay time. The counter should keep counting continously. If the counter reach more 2 minutes and the next gameover screen appear, the ads should show up and the counter restarting counting from zero.

// For timer Admob
public int counter = 0;
public bool timer = true;

void Update()
{

    if (timer == true)
    {
        counter++;
    }
}

And in my gameover screen

if (counter >= 120) // delay for 2 minutes
{
    <<my custom code to show admob>>
    timer = true;
    counter = 0;// set the counter to zero and restarting count
}
else
{
    timer = true; // it will keep counting
}

Really apreciated for help


Solution

  • I think Update() is called every frame (probably around 30 - 60 times per second) so delay need to be much higher, also to make your game not frame dependant use Time.deltaTime.

    http://docs.unity3d.com/ScriptReference/Time-deltaTime.html

    Edit: Also in shown code, to timer variable you are never assigning false so looks like it's redundant. You're comparing variable delayAdmob in an if statement, but where do you change or declare it?