- Home /
How to fix a Missing Refrence Exception Error
Hi! I'm fairly new to coding and I have been working on expanding the RPG Creator Kit! I added in a menu system that gives you the option to change the volume, and an option to load the game.
However, when I first load the game everything is fine. But the moment I click the main menu button and reload the game scene a "Missing Reference Exception" error occurs.
This is the error that is displayed: MissingReferenceException: The object of type 'FadingSprite' 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.
And finally, this is the code that the error is linked to:
using System.Collections.Generic;
using UnityEngine;
namespace RPGM.Gameplay
{
/// <summary>
/// A system for batch animation of fading sprites.
/// </summary>
public class FadingSpriteSystem : MonoBehaviour
{
void Update()
{
foreach (var c in FadingSprite.Instances)
{
if (c.gameObject.activeSelf)
{
c.alpha = Mathf.SmoothDamp(c.alpha, c.targetAlpha, ref c.velocity, 0.1f, 1f);
c.spriteRenderer.color = new Color(1, 1, 1, c.alpha);
}
}
}
}
}
Answer by mf41z · Jan 13 at 11:50 AM
How are you reloading the scene? As I see it, during the unloading you're deleting a FadingSprite that is used in the script you posted, and you didn't restore that sprite on reload. Would need more info, such as the unloading and reloading methods, and also how you're storing the scenario.
Hi! Thanks for the response. The only two scripts I could assume be affecting it are the SceneSwitcher scripts. I'll link them in this post but I also thought of something while writting this, what if I put in a line of code that basiclly checks if FadingSprite is null like the error says. If that would work, how could I implement that?
I appreciate all the help and here is the SceneSwitcher script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class SceneLoader : MonoBehaviour
{
public void LoadGame()
{
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1);
}
public void QuitGame()
{
Application.Quit();
Debug.Log("QUIT BUTTON PRESSED!");
}
}
No problem! That would work, yeah. You can always check if a reference is null before using it. The problem with that as I see is that you lose that ability to quickly find out in your code where there is a missing reference, making it harder to debug odd behaviours. But in any case, what you could do to check if the ref is null is something like:
foreach (var c in FadingSprite.Instances)
{
if(c.gameObject == null) return;
if (c.gameObject.activeSelf)
{
c.alpha = Mathf.SmoothDamp(c.alpha, c.targetAlpha, ref c.velocity, 0.1f, 1f);
c.spriteRenderer.color = new Color(1, 1, 1, c.alpha);
}
}
That would quit the method before the reference is used. I'm not sure if that's where the problem is, but before you make use of the FadingSprite in code, just do a if(FadingSprite == null) return;