Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Reset Scene MissingReferenceException:

Discussion in 'Scripting' started by deatheragenator, Aug 29, 2023.

  1. deatheragenator

    deatheragenator

    Joined:
    Dec 31, 2018
    Posts:
    5
    I'm getting an error only after a scene reset where A mesh no longer resets the value to true. I get no errors before i reset the scene. When A scene has been reloaded all values reset to the default and preferred state.
    Code (CSharp):
    1. public class AirplaneController : MonoBehaviour
    2. {
    3. if (Input.GetKeyDown(KeyCode.R))
    4.         {
    5.             SceneManager.LoadSceneAsync(SceneManager.GetActiveScene().name);
    6.             Debug.Log("Scene Reset");
    7.         }
    8. }
    This code correctly processes after a reset.

    Code (CSharp):
    1.  
    2.  
    3. public class LapController : MonoBehaviour
    4. {
    5. public MeshRenderer mesh;
    6. if (player.gameObject.tag == "Player" && active)
    7.         {
    8. if (checkpoints == NumberOfCheckpoints)
    9.             {
    10.                 checkpoints = 0;
    11.  
    12. foreach (var instance in CheckpointController.Instances)
    13.                 {
    14.                     instance.ResetCP(checkpoints);
    15.                     Debug.Log("Send Reset");
    16.                 }
    17.              }
    18.     }
    19. }
    The rest of this script works correctly but the ResetCP does nothing. If i remove the mesh.enabled = true; no error messages occur.

    Code (CSharp):
    1. public class CheckpointController : MonoBehaviour
    2. {
    3. public void ResetCP(int checkpoints)//from LapController.cs
    4.     {
    5.         CpCount = checkpoints;
    6.         active = true;
    7.         mesh.enabled = true;
    8.         Debug.Log("Reset Value");
    9.      
    10.     }
    11. }
    Error:
    MissingReferenceException: The object of type 'MeshRenderer' 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.

    Other similar issues
    https://www.reddit.com/r/unity/comments/w0i4so/script_wont_work_after_reloading_a_scene/
    https://forum.unity.com/threads/reloading-scene-causes-update-function-to-stop-working.760706/
    https://discussions.unity.com/t/how-to-restart-scene-properly/118396
     
    Last edited: Aug 29, 2023
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,718
    Seems pretty clear that whatever
    mesh
    is, it's an object that existed in the scene that was unloaded when you reloaded the scene.

    If you're using DDOL objects or event subscriptions you need to make sure you're not holding onto stale references from unloaded scenes. Else you will get such errors when you try to access the already destroyed objects.
     
  3. deatheragenator

    deatheragenator

    Joined:
    Dec 31, 2018
    Posts:
    5
    Sorry i missed it in the code mesh is the MeshRenderer.
    When i reload the MeshRenderer = true. Then it's set to false on a trigger enter. It's only after it's deactivated that i can't re-enable it. Not using DDOL.
     
  4. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,718
    This is all just noise.
    The error is clear - you are accessing an object that was destroyed. Your focus in the investigation should be tracking down which reference that is and why it's still around in the new scene. It seems like it's this
    mesh
    reference or even the CheckpointController that reference is on. I will say:
    Code (CSharp):
    1. foreach (var instance in CheckpointController.Instances)
    This is extremely suspect. Is that a static list of CheckpointController instances? How / when is it populated? Static variables don't know or care about scenes so it's quite likely that list still refers to objects from the previous scene.
     
    deatheragenator likes this.