Unity Ads Initializing not working

Unity version : 2020.3.25f1
Advertisement 3.7.5

Hi,

I can't manage to get the ads initializer script to work, the OnInitiliazationComplete or OnInitializeationFailed are never fired.

Build settings is on Android
Ads Service is on.
Project ads is set in unity dashboard
Game ID for android and ios is correctly set in Projet Settings and on the gameobject with the ads initializer class.
Testmode is set to true in the game object inspector.
Debug log in awake and in InitializeAds can be seen when in Play.

using UnityEngine;
using UnityEngine.Advertisements;
public class AdsInitializer : MonoBehaviour, IUnityAdsInitializationListener
{
    [SerializeField] string _androidGameId;
    [SerializeField] string _iOSGameId;
    [SerializeField] bool _testMode = true;
    private string _gameId;
    void Awake()
    {
        InitializeAds();
        Debug.Log("Start initializer");
    }
    public void InitializeAds()
    {
        _gameId = (Application.platform == RuntimePlatform.IPhonePlayer)
            ? _iOSGameId
            : _androidGameId;
        Advertisement.Initialize(_gameId, _testMode, this);
        Debug.Log("Initializer has been fired");
    }
    public void OnInitializationComplete()
    {
        Debug.Log("Unity Ads initialization complete.");
    }
    public void OnInitializationFailed(UnityAdsInitializationError error, string message)
    {
        Debug.Log($"Unity Ads Initialization Failed: {error.ToString()} - {message}");
    }
}

Thanks

I've kept working to try to see if playing ads would function despite not having initialization feedback and it works as intented, so I guess it might just be a bug on the initialization callback

Hi @jdg23 Are you using the embedded unity ads SDK version in Unity by just turning on Unity Ads on the service window?
Anyway, you have to update unityads SDK to at least 4.0.1 (or upper than 4.0.1) to comply with google's data policy.
So could you download unityads SDK through the package manager and try to run your code again?
Please refer to this to know how to get unityads SDK through package manager
https://docs.unity.com/ads/en/manual/InstallingTheUnitySDK

Hi,

Thank you for your answer.

As I said in my first answer, I saw that the ads actually worked as intented despite not having initialization confirmation at the launch of the app so I thought that problem was resolved.

But as you indicated, you recommend to update the SDK to at least version 4.0.1. When we first imported the Advertissement package, we saw in Project Settings / Services/ Ads that we could update it from 3.7.5 to 4.4.1 which actually is a different package if I understood correctly, with different functions.

For instance, after upgrading to 4.4.1, the initialization script was not working anymore as IUnityAdsInitializationListener does not seem to be in the new version.

I was able to revert back to a previous version of the project with source control but if you say that we really need to upgrade the package, then what would be the new code for initialization and playing ads?

Thanks

Hi @jdg23 IUnityAdsInitializationListener should work on AdsSdk 4.4.1
Could you test your script with live ads on a real device? (in your code, _testMode=false)

Hi again,
Thank you, it's working. My mistake was that a pop up appeared asking me if I wanted to use the new ad with mediation or the legacy one and it was because I chose to use the new ad with mediation instead of the legacy.

There is not much you can do with the above code.
Couple of listeners are missing.
Here is a code snippet which works well in the test mode, even if you wish to show ads multiple times.

using UnityEngine;
using UnityEngine.Advertisements;

public class AdsInitializer : MonoBehaviour, IUnityAdsInitializationListener, IUnityAdsLoadListener, IUnityAdsShowListener
{
    [SerializeField] string _androidGameId;
    [SerializeField] string _iOSGameId;
    [SerializeField] bool _testMode = true;
    private string _gameId;

    [SerializeField] string _androidAdUnitId = "Interstitial_Android";
    [SerializeField] string _iOsAdUnitId = "Interstitial_iOS";
    string _adUnitId;

    void Awake()
    {
        _adUnitId = (Application.platform == RuntimePlatform.IPhonePlayer)
            ? _iOsAdUnitId
            : _androidAdUnitId;

        InitializeAds();
    }

    public void InitializeAds()
    {
        _gameId = (Application.platform == RuntimePlatform.IPhonePlayer)
            ? _iOSGameId
            : _androidGameId;

        if (!Advertisement.isInitialized) {
            Advertisement.Initialize(_gameId, _testMode, this);
        }
        else {
            LoadAd();
            ShowAd();
        }
    }

    public void OnInitializationComplete()
    {
        LoadAd();
    }

    public void OnInitializationFailed(UnityAdsInitializationError error, string message)
    {
        Debug.Log($"Unity Ads Initialization Failed: {error.ToString()} - {message}");
    }

    // Load content to the Ad Unit:
    public void LoadAd()
    {
        // IMPORTANT! Only load content AFTER initialization (in this example, initialization is handled in a different script).
        Advertisement.Load(_adUnitId, this);
    }

    // Show the loaded content in the Ad Unit:
    public void ShowAd()
    {
        // Note that if the ad content wasn't previously loaded, this method will fail
        Advertisement.Show(_adUnitId, this);
    }

    // Implement Load Listener and Show Listener interface methods:
    public void OnUnityAdsAdLoaded(string adUnitId)
    {
        // Optionally execute code if the Ad Unit successfully loads content.
        ShowAd();
    }

    public void OnUnityAdsFailedToLoad(string adUnitId, UnityAdsLoadError error, string message)
    {
        Debug.Log($"Error loading Ad Unit: {adUnitId} - {error.ToString()} - {message}");
        // Optionally execute code if the Ad Unit fails to load, such as attempting to try again.
        //TODO go to next scene instead
    }

    public void OnUnityAdsShowFailure(string adUnitId, UnityAdsShowError error, string message)
    {
        Debug.Log($"Error showing Ad Unit {adUnitId}: {error.ToString()} - {message}");
        // Optionally execute code if the Ad Unit fails to show, such as loading another ad.
        //TODO go to next scene instead
    }

    public void OnUnityAdsShowStart(string adUnitId) {
    }
    public void OnUnityAdsShowClick(string adUnitId) {
    }
    public void OnUnityAdsShowComplete(string adUnitId, UnityAdsShowCompletionState showCompletionState) {
        SceneManager.LoadScene("SceneName", LoadSceneMode.Single);
    }



}
1 Like

Hello. I installed 4.4.1 Unity Sdk and is seems that ads initialization never completes:

using UnityEngine;
using UnityEngine.Advertisements;

public class AdsInitializer : MonoBehaviour, IUnityAdsInitializationListener, IUnityAdsLoadListener, IUnityAdsShowListener
{

    [SerializeField] string _iOSGameId;
    [SerializeField] bool _testMode = true;
    private readonly string _adUnitId = "Interstitial_iOS";

    void Awake()
    {
        InitializeAds();
    }

    public void InitializeAds()
    {

        if (!Advertisement.isInitialized)
        {
            Advertisement.Initialize(_iOSGameId, _testMode, this);
            Debug.Log("ADS: Advertisement.Initialize launched");
        }
        else
        {
            LoadAd();

        }



    }

    // Show the loaded content in the Ad Unit:
    public void ShowAd()
    {

        Debug.Log("ADS: ShowAd launched!");
        // Note that if the ad content wasn't previously loaded, this method will fail
        Advertisement.Show(_adUnitId, this);
    }



    // Load content to the Ad Unit:
    public void LoadAd()
    {
        Debug.Log("ADS: in LoadAd");
        // IMPORTANT! Only load content AFTER initialization (in this example, initialization is handled in a different script).

        if (Advertisement.isInitialized)
        {

            Advertisement.Load(_adUnitId, this);
            Debug.Log("ADS: Advertisement.Load launched");
        }
        else
        {
            InitializeAds();
        }
    }

    void IUnityAdsInitializationListener.OnInitializationComplete()
    {
        Debug.Log("ADS: INITIALIZATION COMPLETE!");
        LoadAd();
    }

    void IUnityAdsInitializationListener.OnInitializationFailed(UnityAdsInitializationError error, string message)
    {
        Debug.Log("ADS: in OnInitializationFailed");
        InitializeAds();
    }

    void IUnityAdsLoadListener.OnUnityAdsAdLoaded(string placementId)
    {
        Debug.Log("ADS: Ads loaded!");

    }

    void IUnityAdsLoadListener.OnUnityAdsFailedToLoad(string placementId, UnityAdsLoadError error, string message)
    {
        LoadAd();
    }

    void IUnityAdsShowListener.OnUnityAdsShowFailure(string placementId, UnityAdsShowError error, string message)
    {
        LoadAd();
    }

    void IUnityAdsShowListener.OnUnityAdsShowStart(string placementId)
    {

    }

    void IUnityAdsShowListener.OnUnityAdsShowClick(string placementId)
    {

    }

    void IUnityAdsShowListener.OnUnityAdsShowComplete(string placementId, UnityAdsShowCompletionState showCompletionState)
    {
        LoadAd();

    }
}

You are missing ShowAd on the line 28.

Is there any particular reason that you decided to change my code snippet ?
Have you tried it out first ?
Even if you will release only on iOS it will still function properly.
Did you setup the _iOSGameId in the Unity Editor ?

Which version of the Unity Editor are you using ?
I have the Advertisement sdk 4.3.0 in the Package Manager.

[quote=“ShinyOpal”, post:9, topic: 906767]
You are missing ShowAd on the line 28.

Is there any particular reason that you decided to change my code snippet ?
Have you tried it out first ?
Even if you will release only on iOS it will still function properly.
Did you setup the _iOSGameId in the Unity Editor ?

Which version of the Unity Editor are you using ?
I have the Advertisement sdk 4.3.0 in the Package Manager.
[/quote]

Hello, thanks for your message.
I have 2021.3.17f1. And Advertisement Legacy 4.4.1
One month ago the iOS build didn’t have PODS folder and the ads worked. Now the build comes with PODS, unity mediation and I think that this is the cause of the problem. I’m not an expert in iOS XCODE, so I can’t change the code to go back to a build without pods

I am using 2021.3.18f1, however I ditched the Advertisement Legacy and use the Advertisement 4.3.0 instead.
The code above works with the Advertisement 4.3.0.
Have not tested it with the Advertisement Legacy.
Somewhere I read that Unity recommends using Advertisement instead of the Advertisement Legacy.

PODS sounds like the CocoaPods, the dependency manager. Very unlikely that could make the problem.

Hi

This line Advertisement.Initialize(myGameIdIOS, testMode, this); doesn't work in APK. I created a loop to run this each 2 seconds and when I try to start the banner, it shows "UnityAds is not initialized" (OnBannerError)

In Editor is just fine.

1 Like

[quote=“heltonluizsb”, post:12, topic: 906767]
Hi

This line Advertisement.Initialize(myGameIdIOS, testMode, this); doesn’t work in APK. I created a loop to run this each 2 seconds and when I try to start the banner, it shows “UnityAds is not initialized” (OnBannerError)

In Editor is just fine.
[/quote]

I had an initialization problem on android too, but it turns out it was an internet problem. (my internet worked fine, as it went through perfectly on the editor) but I had a VPN running on my phone. So I turned off the VPN and it worked. Also heard someone else say that it didn’t work when testing on a public network. Hopefully this helps someone