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

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.

@mf41z

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

I also have the same problem, what I did was I added DontDestroyOnLoad on the parents of the objects containing FadingSprite. In RPG Creator Kit! I think i’s the “Environment” and “World” GameObject and that removed all my errors. Hope this help.