So I had to integrate Unity Ads to my game, and because of the way everything is structured and my lack of knowledge on ads integration, I set up a bit of a confusing system. I will have to explain it best I can so that my question makes more sense (I will also be including the codes which I am about to mention):
On game launch, it always opens on an empty scene which then directs the player to whichever scene they were in in their save file.
There is the AdsInitializer script in the entrance scene and also a DontDestroyOnLoad script attached to the same gameobject.
In every other scene, there is the InterstitialAdsButton script attached to all the buttons that need to invoke the ad when pressed. I have a list of objects of which InterstitialAdsButton script is attached to. The list is in AdsInitializer and it updates every time we go into a new scene.
There is a UI master script that handles all of the buttons. I add and remove the buttons to the list, which is in AdsInitializer, in this script. It clears the list on Start and adds the right ones in that scene, so that we don’t have leftover buttons or empty list items from the previous scene.
I show the ads when a button is clicked through the UI script.
So far, so good. When I play in the editor, the placeholder screen for the ads do show. However, I receive the error “placementId cannot be nil or empty” for every ad that I have in the scene. But this only happens in the first level that I load. As I progress through the game, or even if I die and hit Restart, it doesn’t give that error anymore. I am suspecting that it is because it takes some time between the ads list to get cleared and then updated, so that’s why the error shows? But that doesn’t really explain why it only happens once. Would this even be a big deal at all if I published the game like this? Either way, how would I go about fixing it?
Here are the codes:
AdsInitializer.cs
using UnityEngine;
using UnityEngine.Advertisements;
using System;
using System.Collections.Generic;
public class AdsInitializer : MonoBehaviour, IUnityAdsInitializationListener
{
private static AdsInitializer adsInstance;
[SerializeField] string _androidGameId;
[SerializeField] string _iOSGameId;
[SerializeField] bool _testMode = true;
private string _gameId;
public List<InterstitialAdsButton> adsList;
void Awake()
{
DontDestroyOnLoad(this);
if (adsInstance == null)
{
adsInstance = this;
}
else
{
Destroy(gameObject);
}
InitializeAds();
}
private void Start()
{
print("started");
}
public void InitializeAds()
{
_gameId = (Application.platform == RuntimePlatform.IPhonePlayer)
? _iOSGameId
: _androidGameId;
Advertisement.Initialize(_gameId, _testMode, this);
}
public void OnInitializationComplete()
{
Debug.Log("Unity Ads initialization complete.");
for(int i = 0; i < adsList.Count; i++) { adsList[i].LoadAd(); }
}
public void OnInitializationFailed(UnityAdsInitializationError error, string message)
{
Debug.Log($"Unity Ads Initialization Failed: {error.ToString()} - {message}");
}
}
StartStopUI.cs (only part of it, since the whole script is irrelevant and much longer)
public class StartStopUI : MonoBehaviour
{
//Ads
InterstitialAdsButton[] int_ads;
AdsInitializer adsIn;
private void Awake()
{
adsIn = GameObject.FindObjectOfType<AdsInitializer>();
adsIn.adsList.Clear();
int_ads = GameObject.FindObjectsOfType<InterstitialAdsButton>(true);
for (int i = 0; i < int_ads.Length; i++)
{
if (!adsIn.adsList.Contains(int_ads[i]))
{
adsIn.adsList.Add(int_ads[i]);
}
}
}
public void GoToNextScene()
{
for (int i = 0; i < int_ads.Length; i++)
{
if (int_ads[i].gameObject.name.Equals("StopButton"))
{
int_ads[i].ShowAd();
}
}
AnalyticsManager.instance.OnLevelComplete(level);
print(scene.buildIndex + " . . . " + SceneManager.sceneCountInBuildSettings);
Iterate();
SceneManager.LoadScene(scene.buildIndex + 1);
}
public void Restart()
{
for (int i = 0; i < int_ads.Length; i++)
{if (int_ads[i].gameObject.name.Equals("RestartButton"))
{
int_ads[i].ShowAd();
}
}
AnalyticsManager.instance.OnLevelFail(level);
SceneManager.LoadScene(scene.name);
}
}
The whole thing is pretty confusing to me so I tried to tell it as simple as possible. My apologies if I somehow made it worse.