Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Why am I getting this MissingReferenceException!?

Discussion in 'Scripting' started by JohnnyLeek9, Sep 16, 2019.

  1. JohnnyLeek9

    JohnnyLeek9

    Joined:
    Sep 16, 2019
    Posts:
    1
    Hello everyone! This is my first post on here as I have looked everywhere and cannot find a similar enough situation!

    I am following along (somewhat) with the Unity Official 2D Roguelike tutorial, and I have basically gotten through the entire thing, however for some reason I am getting an odd MissingReferenceException when I change levels. The GameController has "DontDestroyOnLoad" enabled, and in the attached gif, you'll see that it doesn't get destroyed. Further, I would like you to pay attention to the inspector, where the Level integer does in fact increment, but when I output it into the console, it outputs "1". Then once I get to the third level, I receive the exception and it makes the game unplayable from there. Any and all help would be greatly appreciated. Thank you!

    Awake function:

    Code (CSharp):
    1.     void Awake()
    2.     {
    3.         Debug.Log(level);
    4.         if (instance == null)
    5.             instance = this;
    6.         else if (instance != this)
    7.             Destroy(gameObject);
    8.  
    9.         DontDestroyOnLoad(this);
    10.  
    11.         enemies = new List<Enemy>();
    12.         worldGenerator = GetComponent<WorldGenerator>();
    13.  
    14.         SceneManager.sceneLoaded += SceneLoaded;
    15.     }
    Gif of the issue in action:
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,186
    Without seeing where the error is occurring, it's impossible for us to know what is going on. (ie, what the code is that is trying to access GameController and throwing the error)

    My guess is that you maybe have a GameController in each scene and the one in the scene is being referenced, but then destroyed, thus you are getting the error because your reference isn't pointing to the one that is floating between scenes.
     
  3. JoshuaMcKenzie

    JoshuaMcKenzie

    Joined:
    Jun 20, 2015
    Posts:
    916
    Awake only fires once per instance. Ever. The fact that you are seeing that debug shows that each scene has its own gamecontroller copy and its that copy thats sending out the debug logs, and why its always printing 1.

    The missing ref exception comes from the fact that something in the scene is referencing that non-"dontdestroyonload" copy and tries to reference it after it was deleted. its not the one that you've set with dontdestroyonload thats causing the missing ref exceptions.

    also you're not returning early when you call destroy(). so the to-be-destroyed copies are adding handlers to the sceneLoaded event. and thats what likely is causeing the missing refs. Remember when you add an event listener, YOU must also remove it, things like the SceneManager aren't going to clean that up for you. If you add in OnEnable, you should remove in OnDisable. if you add in Awake/Start, you should remove in OnDestroy. if you add in practically any other way, you invite yourself to some headaches when you have to think about all the cases where you need to clean up that listener.