Missing Reference Exception using Unity Ads on second run of game

I’m building a game which uses Ads that run when a player returns to the main menu. The first time I run the game the ads run as intended and the script works, however the second time I try to run it, it produces the following error message:

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@4.4.2/Runtime/Advertisement/Platform/Editor/Placeholder.cs:187)
UnityEngine.Advertisements.Placeholder.Show (System.String placementId, System.Boolean allowSkip) (at Library/PackageCache/com.unity.ads@4.4.2/Runtime/Advertisement/Platform/Editor/Placeholder.cs:74)
UnityEngine.Advertisements.Platform.Editor.EditorPlatform+<>c__DisplayClass14_0.b__0 () (at Library/PackageCache/com.unity.ads@4.4.2/Runtime/Advertisement/Platform/Editor/EditorPlatform.cs:165)
UnityEngine.Advertisements.Utilities.CoroutineExecutor.Update () (at Library/PackageCache/com.unity.ads@4.4.2/Runtime/Advertisement/Utilities/CoroutineExecutor.cs:17)

Using Debug.Log I suspect the ads may not be initializing properly the second time I run the game but have no clue on what the actual problem is.
I am using Unity Ads (not mediation/Levelplay) and have updated my ads to the most recent version. My Ads script is as follows:

using UnityEngine;
using UnityEngine.Advertisements;
using UnityEngine.SceneManagement;
using System.Collections;
using System.Collections.Generic;

public class InterstitialAdExample : MonoBehaviour, IUnityAdsLoadListener, IUnityAdsShowListener, IUnityAdsInitializationListener
{
    [SerializeField] string _iOsAdUnitId = "Interstitial_iOS";
    string _adUnitId;

    public bool isTestingMode = true;

    void Start()
    {
        StartCoroutine(DelayedInitialization());
    }

    IEnumerator DelayedInitialization()
    {
        yield return new WaitForSeconds(2.0f); // Adjust the delay as needed
        Advertisement.Initialize("********", isTestingMode, this);
    }

    void Awake()
    {

        // Get the Ad Unit ID for the current platform:
        _adUnitId = _iOsAdUnitId;

        LoadAd();
    }

    // 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).
        Debug.Log("Loading Ad: " + _adUnitId);
        Advertisement.Load(_adUnitId, this);
    }

    // Show the loaded content in the Ad Unit:
    public void ShowAd()
    {
        Debug.Log("Showing Ad: " + _adUnitId);
        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.
    }

    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.
    }

    public void OnUnityAdsShowFailure(string _adUnitId, UnityAdsShowError error, string message)
    {
        Debug.Log($"Error showing Ad Unit {_adUnitId}: {error.ToString()} - {message}");
    }

    public void OnUnityAdsShowStart(string _adUnitId) { }
    public void OnUnityAdsShowClick(string _adUnitId) { }
    public void OnUnityAdsShowComplete(string _adUnitId, UnityAdsShowCompletionState showCompletionState)
    {
        SceneManager.LoadScene("MainMenu");
    }


    public void OnInitializationComplete()
    {
        print("Ads initialized!!");
    }

    public void OnInitializationFailed(UnityAdsInitializationError error, string message)
    {
        print("failed to initialize!!");
    }
}

I appreciate the code has not been written in the most optimal way but have tried various different methods of using the Ads and the same problem always arises.

Thank you for any help (I hope I have posted this issue in the correct place, I apologies if not)

Could you check if this error will also occur on a real device?

Having built my game for iOS and played it on an iphone, this issue no longer appears. Thank you very much.

1 Like

This error also happens for me in the editor. I haven’t tried building it yet like @williamxhal did, but it is very annoying in the editor.

Edit: it randomly started working again. not sure why