How to make unity ads show every 3 death? Please be specific, thank you.

I have the default unity ads set, which is shown once every time the “GameOver” scene is loaded. The user’s feedback are saying its too much! I would like to set the ads so they are shown once every three times the “GameOver” scene is loaded, please be specific and detailed, I am not very good at C#. Thanks.
This is my script.

2663568–187867–ads.cs (364 Bytes)

2 Likes

If I’ve understood you correctly, this should do it:

using UnityEngine;
using UnityEngine.Advertisements;

public class Ads : MonoBehaviour
{
    static int loadCount = 0;

    void Start()
    {
        if (loadCount % 3 == 0)  // only show ad every third time
        {
            ShowAd ();
        }
        loadCount++;
    }

    public void ShowAd()
    {
        if (Advertisement.IsReady())
        {
            Advertisement.Show();
        }
    }
}

Let me know if it works.

-Rasmus

10 Likes

It worked perfectly, thank you sooo much! your a legend!

2 Likes

how did you trigger this script?

did you just put it on a game object or on a button or something?

(im a real noob sorry)

@phyzikalgamer , just attach the script to any game object in your scene, and it should show ad every third time scene is loaded.

You can also consider reading https://blogs.unity3d.com/2015/04/15/a-designers-guide-to-using-video-ads/ for some best practices on integrating ads into your game. Showing on scene load, might not give the best user experience.

/Rasmus

Legend, thank you!

Sorry one quick question i have this setup and it works well (I changed to every 6 loads) only issue i have it when i click start game it plays add then i die 6 times then it loads.

Its like it always plays the ad once first then every 6 times. Is there a way to stop it playing the first time and only after the first x number of deaths

using UnityEngine;
using UnityEngine.Advertisements;
public class ads : MonoBehaviour
{
    static int loadCount = 1;
    void Start()
    {
        if (loadCount % 6+1 == 0)  // only show ad every third time
        {
            ShowAd ();
        }
        loadCount++;
    }
    public void ShowAd()
    {
        if (Advertisement.IsReady())
        {
            Advertisement.Show();
        }
    }
}
2 Likes

There is a better way to do it, this way it will continue counting without resetting the timer this is a better one

static int loadCount = 0;

void Start()
{

if (loadCount == 4)
{
loadCount = 0;
ShowAd();
}
else
{
loadCount++;
}

}

public void ShowAd()
{
if (Advertisement.IsReady())
{
Advertisement.Show();
}
}

1 Like

Great thread, solved the issue I was having.

Having to say that coming from someone that has no real knowledge of coding the Unity Tutorials and Community has been invaluable.

1 Like

Hello Guys
I’m also not very familiar with showing ads
I have successfully showed unity ads with the method in the first comments thanks to rasmus-unity and NikolaTotev
but I still struggle with other network ads such as admob and chartboost, Their SDK is kind of hard to me.
I want to do the same thing with admob (Showing Interstitial Ads After 3 gameplays), I’m using a plugin from unity asset store called “very easy ads” but i still can’t implement ads correctly.
If someone already did what i want to do, please help me show ads too

I do not understand “resetting the timer”. What timer and what function does it serve?

I have reopened my game to try getting ads to work and cannot even get started on ads because of this error.
“UnityException: Transform child out of bounds
PlayerScript.CreateAtStart (Int32 amount) (at Assets/Walls/Scripts/PlayerScript.cs:297)
PlayerScript.Start () (at Assets/Walls/Scripts/PlayerScript.cs:66)”
methos5k tried to help but nothing seems to work.
Link to that thread : UnityException: Transform child out of bounds PlayerScript.CreateAtStart (Int32 amount) (at Assets/W - Unity Engine - Unity Discussions

Does this work with Android also?
@rasmus-unity

I rebuilt my game from scratch.
No more player script error.
I am now getting the following error using the script by rasmus-unity.
"
UnityAdsEditor: Initialize(gameID, True);
UnityEditor.Advertisements.UnityAdsEditor:EditorOnLoad()"

The test ad does not show.
I have build setting to use Android.

Is there a way to fix this?
I am using Unity 5.6.5f1

@incenseman , are you using Asset Store or engine integrated version of Ads SDK? And what do you mean by errors, does they have the red error icon next to them (looks like those messages are from Console window in editor)

/Rasmus

I am using the integrated ads SDK. It is in from Console window in editor and has an exclamation point in a white text bubble like from a comic strip.

I have removed all ad related scripts and still get the same thing.
The only way I do not see this issue is when ads are off.

Re-added your script and now am getting this:
“error CS0234: The type or namespace name Advertisements' does not exist in the namespace UnityEngine’. Are you missing an assembly reference?”

Here is the line of code that takes me to:

using UnityEngine.Advertisements;

Then it’s not an error, errors have red exclamation marks, see Unity - Manual: Console Window

With the log messages above, it seems that Unity Ads SDK is initialized in your case, so I don’t know why you don’t see test ad in editor.

We have an example of engine integrated ads at GitHub - Unity-Technologies/unity-ads-engineintegration-test. Hope that might give you some idea what the issue is in your case.

/Rasmus

Whats the “intregate ads SDK” ? I’m confused with that I think.
Thanks.

“engine integrated ads” means when you are using the Services window in Unity editor to enable ads, see “Services Window method” section on Unity developer integration guides

Hope that answers your question.

/Rasmus