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);
}
}