- Home /
MissingReferenceException, please help :(
Okay so i spend hours trying to fix this and i still have no clue why is this error ocuring. I have button that when clicked plays ad and mute audio listener and then after ad is finished it automaticly enable audiolistener and some other stuff, this all works but when i restart my level using GetActiveSccene, the button works anymore as it plays the Ad but it wont mute the audiolistener instead gives me this error :
MissingReferenceException: The object of type 'AudioListener' 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.
Unfortunately this block my other function connected to this. I have no idea how is this when im not either destroying audilistener object or disabling it, and when the scene is first played from editor it works perfectly fine so the error occurs somewhere when my lvl is reloading.
My AdManager script :
public AudioListener listener;
public SecondChance sc;
private string playStoreID = "3840117";
private string appStoreID = "3840117";
private string rewardedAd = "rewardedVideo";
public bool isTargetPlayStore;
public bool isTestAd;
private void Start()
{
Advertisement.AddListener(this);
InitializeAdvertisement();
}
void Update()
{
listener = GameObject.Find("Main Camera").GetComponent<AudioListener>();
}
void InitializeAdvertisement()
{
if(isTargetPlayStore)
{
Advertisement.Initialize(playStoreID, isTestAd); return ;
}
Advertisement.Initialize(appStoreID, isTestAd);
}
public void PlayRewardedVideoAD()
{
if (!Advertisement.IsReady(rewardedAd)) { return; }
Advertisement.Show(rewardedAd);
}
public void OnUnityAdsDidStart(string placementId)
{
listener.enabled = false;
}
public void OnUnityAdsDidFinish(string placementId, ShowResult showResult)
{
switch (showResult)
{
case ShowResult.Failed:
Debug.LogWarning("Something went wrong with an Advertisement");
break;
case ShowResult.Skipped:
break;
case ShowResult.Finished:
if(placementId == rewardedAd)
{
Debug.Log("Reward The player");
listener.enabled = true;
sc.SCAnim();
}
if(placementId == interstitialAd)
{
Time.timeScale = 1;
Debug.Log("Finished interstitial");
}
break;
}
}
And my Script from which i controll buttons:
public Rigidbody rb;
public Text WatchAD;
public Text Needxchance;
public Animator anim;
public Animator animScore;
public AudioSource windFx;
public InfinitePlayerMovement plmvmt;
public InfinitePlayerCollision plc;
public InfiniteLevelScore inflsc;
public Button second;
public Button restartBut;
public Button menuBut;
public float chances;
public float playedAds;
public float titletext;
public bool elevated;
private void Start()
{
second.enabled = false;
restartBut.enabled = false;
menuBut.enabled = false;
playedAds = 0f;
}
private void Update()
{
//Title Text
titletext = chances + 1;
if (titletext > 2)
{
if (titletext == 3)
{
Needxchance.text = "Need a third chance ?";
}
else
{
Needxchance.text = "Need a " + titletext + "th chance ?";
}
}
WatchAD.text = "Watch AD " + playedAds + "/" + chances;
chances = plc.Crashes;
elevated = plmvmt.cubeIsOnTheLeveledGround;
}
//Triggered by InfinitePlayerCollision
public void SecondChanceScreen()
{
animScore.SetTrigger("TextFade");
anim.SetTrigger("SecondChance");
Time.timeScale = 0f;
windFx.Stop();
second.enabled = true;
restartBut.enabled = true;
menuBut.enabled = true;
plmvmt.cubeIsOnTheGround = false;
}
public void AdPlayed()
{
playedAds += 1;
Invoker.InvokeDelayed(SCAnim, 0f);
}
public void SCAnim()
{
plmvmt.OnlyOnce = false;
playedAds += 1;
if (playedAds == chances)
{
anim.SetTrigger("SecondChanceClose");
Invoker.InvokeDelayed(SecondChanceOpen, 0.7f);
}
}
public void SecondChanceOpen()
{
playedAds = 0;
animScore.SetTrigger("TestClip");
Debug.Log("2ndChance");
//Physical changes to Player
rb.velocity = new Vector3(0f, 0f, 50f);
if (elevated == false)
{
rb.transform.position = new Vector3(0f, 20f, rb.transform.position.z);
}
if (elevated == true)
{
rb.transform.position = new Vector3(0f, 35f, rb.transform.position.z);
}
rb.transform.rotation = Quaternion.Euler(0, 0, 0);
Time.timeScale = 1f;
second.enabled = false;
restartBut.enabled = false;
}
public void Restart()
{
Debug.Log("Restart");
FindObjectOfType<InfiniteGameManager>().Restart();
Time.timeScale = 1f;
second.enabled = false;
restartBut.enabled = false;
menuBut.enabled = false;
}
public void Menu()
{
Debug.Log("Menu");
second.enabled = false;
restartBut.enabled = false;
menuBut.enabled = false;
inflsc.scoreIncreasing = false;
}
I found out that when i removed the listener function, it started writing error on Animator in the second script and when i tried to move audilistener component or move the listener function, it still made this error but with just diffferent script linked to it. Please help im desperate and i wasted countless hours on this problem. :(((((
Answer by FogCZ · Oct 06, 2020 at 09:40 PM
Just setting variables static fixed the issue and moved it somewhere else, but its a step and im on way to fix the rest of issues.