Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by PersonWhoLikesToCode · Jan 13 at 02:09 AM · scripting problemscene-loadingdebugreloadmissingreferenceexception

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);
                 }
             }
         }
     }
 }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image PersonWhoLikesToCode · Jan 13 at 01:53 PM 0
Share

@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!");
          }
  }
  
avatar image mf41z PersonWhoLikesToCode · Jan 13 at 09:04 PM 0
Share

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;

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

237 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

MissingReferenceException on Inspector-assigned values after reloading scene 1 Answer

FPS Measurement Issue 0 Answers

How do you deactivate a static script until a certain key is pressed? 2 Answers

I want music to continuously play even when the scene restarts 1 Answer

Load Scene Bug 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges