Help with MissingReferenceException

Hello,

I am making a puzzle game with several scenes and in each scene, the player can get a hint by watching a rewarded ad. Everything works fine in the first scene, but when I upload the second scene, after the video the following error appears:
"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.
AdManager.EnableDica () (at Assets / Scripts / AdManager.cs: 140)
AdManager.OnUnityAdsDidFinish (System.String placementId, UnityEngine.Advertisements.ShowResult showResult) (at Assets / Scripts / AdManager.cs: 101)
UnityEngine.Advertisements.Platform.Platform + <> c__DisplayClass41_0. b__0 () (at Library/PackageCache/com.unity.ads@3.5.2/Runtime/Advertisement/Platform/Platform.cs: 171)
UnityEngine.Advertisements.Utilities.CoroutineExecutor.Update () (at Library/PackageCache/com.unity.ads@3.5.2/Runtime/Advertisement/Utilities/CoroutineExecutor.cs: 17)
"
I’m new to Unity and I couldn’t replicate any solution I found on the internet. Can anyone point me to what I may be missing?

using System.Collections;
using UnityEngine;
using UnityEngine.Advertisements;

public class AdManager : MonoBehaviour, IUnityAdsListener
{
    private string playStoreID = "*******";

    private string interstitialAd = "video";
    private string rewardedVideoAd = "rewardedVideo";
    private string bannerAd = "banner";

    public bool isTestAd;

    public GameObject dica;
    public GameObject confirmaAd;

   

    private void Start()
    {
        Advertisement.AddListener(this);
        InitializeAds();
        DisableConfirmaAd();
        DisableDica();
    }

    private void InitializeAds()
    {
        Advertisement.Initialize(playStoreID, isTestAd);
      
        StartCoroutine(ShowBannerWhenReady());

        Advertisement.Banner.SetPosition(BannerPosition.BOTTOM_CENTER);
    }
    //método do banner
    IEnumerator ShowBannerWhenReady()
    {
        while (!Advertisement.IsReady(bannerAd))
        {
            yield return new WaitForSeconds(0.5f);
        }
        Advertisement.Banner.Show(bannerAd);
    }

    // método da propaganda em vídeo
    public void PlayInterstitialAd()
    {
        if (!Advertisement.IsReady(interstitialAd))
        {
            return;
        }

        Advertisement.Show(interstitialAd);
    }

    //métodos do Rewarded Vídeo
    public void PlayRewardedVideoAd()
    {
        if (!Advertisement.IsReady(rewardedVideoAd))
        {
            return;
        }

        Advertisement.Show(rewardedVideoAd);
    }

    public void OnUnityAdsReady(string placementId)
    {
        //throw new System.NotImplementedException();
    }

    public void OnUnityAdsDidError(string message)
    {
        //throw new System.NotImplementedException();
    }

    public void OnUnityAdsDidStart(string placementId)
    {
        //throw new System.NotImplementedException();
    }

    public void OnUnityAdsDidFinish(string placementId, ShowResult showResult)
    {
        if (showResult == ShowResult.Finished)
        {
            // Reward the user for watching the ad to completion.
            if (placementId == rewardedVideoAd)
            {
                EnableDica();
                DisableConfirmaAd();


            }
            if (placementId == interstitialAd)
            {
                Debug.Log("Finished Interestitial");
            }
        }
        else if (showResult == ShowResult.Skipped)
        {
            DisableConfirmaAd();
            // Do not reward the user for skipping the ad.

        }
        else if (showResult == ShowResult.Failed)
        {
            DisableConfirmaAd();
            Debug.LogWarning("The ad did not finish due to an error.");
            //confirmaAd.GetComponent<Canvas>().enabled = false;
        }

    }

    public void EnableConfirmaAd()
    {
        confirmaAd.SetActive(true);
    }

    public void DisableConfirmaAd()
    {
        confirmaAd.SetActive(false);
    }

    public void EnableDica()
    {
        dica.SetActive(true);
    }

    public void DisableDica()
    {
        dica.SetActive(false);
    }
}

The answer is always the same… ALWAYS. It is the single most common error ever. Don’t waste your life on this problem. Instead, learn how to fix it fast… it’s EASY!!

Some notes on how to fix a NullReferenceException error in Unity3D

  • also known as: Unassigned Reference Exception
  • also known as: Missing Reference Exception

http://plbm.com/?p=221

The basic steps outlined above are:

  • Identify what is null
  • Identify why it is null
  • Fix that.

Expect to see this error a LOT. It’s easily the most common thing to do when working. Learn how to fix it rapidly. It’s easy. See the above link for more tips.

This is the kind of mindset and thinking process you need to bring to this problem:

Step by step, break it down, find the problem.

In the above case, is there something you intended to mark as DontDestroyOnLoad() but did not?

The log shows that the error is in line 127 “dica.SetActive(true);”. It happens when the rewarded ad finishes and the function EnableDica() is called.
I tried to put an script on the GameObject Dica and GameObject ConfirmaAd with

void Awake()
    {
        DontDestroyOnLoad(this);
    }

but then what happens is the EnableDica() shows the hint from the current scene and from the previous scene.