- Home /
Why I get Unity Ads Missing Reference error?
Hi Guys! I started to implement some ads into my game, and I have encountered with a strange but really annoying problem. The game show an Interstitial Ad when the player buy something. This works perfectly for the first time I press the play button, but when I stop the game and start it again the Ad doesn't show up, moreover I get this error:
MissingReferenceException: The object of type 'GameObject' has been destroyed but you are still trying to access it.Your script should either check if it is null or you should not destroy the object.UnityEngine.Advertisements.Placeholder.ShowSkipButton (UnityEngine.GameObject canvasGameObject) (at Library/PackageCache/com.unity.ads@3.7.1/Runtime/Advertisement/Platform/Editor/Placeholder.cs:184)UnityEngine.Advertisements.Placeholder.Show (System.String placementId, System.Boolean allowSkip) (at Library/PackageCache/com.unity.ads@3.7.1/Runtime/Advertisement/Platform/Editor/Placeholder.cs:75)UnityEngine.Advertisements.Platform.Editor.EditorPlatform+<>c_DisplayClass15_0.b_0 () (at Library/PackageCache/com.unity.ads@3.7.1/Runtime/Advertisement/Platform/Editor/EditorPlatform.cs:184)UnityEngine.Advertisements.Utilities.CoroutineExecutor.Update () (at Library/PackageCache/com.unity.ads@3.7.1/Runtime/Advertisement/Utilities/CoroutineExecutor.cs:17)
I don't really understand what's cause it, but I can see that It's not from my script. The only solution I found is reseting the Ads every time after the first try by turning off then on the Ads under Services.
using UnityEngine;
using UnityEngine.Advertisements;
public class InterstitialAdsScript : MonoBehaviour
{
string gameId = "1234567";
string mySurfacingId = "Interstitial_Android";
bool testMode = true;
void Start()
{
// Initialize the Ads service:
Advertisement.Initialize(gameId, testMode);
}
public void ShowInterstitialAd()
{
// Check if UnityAds ready before calling Show method:
if (Advertisement.IsReady(mySurfacingId))
{
Advertisement.Show(mySurfacingId);
}
else
{
Debug.Log("Interstitial ad not ready at the moment! Please try again later!");
}
}
}
I got this script from Unity's website, and I changed the gameID. I attached this script to a gameobject on the scene.
I hope someone can figure this out. Thanks!
Answer by M0SKOV · Nov 24, 2021 at 11:45 AM
You can try this using UnityEngine; using UnityEngine.Advertisements;
public class InterstitialAdsScript : MonoBehaviour {
string gameId = "1234567";
string mySurfacingId = "Interstitial_Android";
bool testMode = true;
private void Start() { Advertisement.AddListener(this); Advertisement.Initialize("1234567", true); }
public void ShowInterstitialAd() { // Check if UnityAds ready before calling Show method: if (Advertisement.IsReady(mySurfacingId)) { Advertisement.Show(mySurfacingId); } else { Debug.Log("Interstitial ad not ready at the moment! Please try again later!"); } }
}
,You can try this
void Start() { // Initialize the Ads service: Advertisement.AddListener(this); Advertisement.Initialize("1234567", true); }