- Home /
MissingReference When Reloading Scene
I am having an issue with variables throwing 'MissingReferenceException' errors after I reload the same scene I just finished playing. I've found other Answers similar to this one but none of those answers have fixed this issue so I'm thinking I might have a different root cause.
I am running a scene that has a 'Retry' button and when the user clicks it I call Application.LoadLevel("MyScene"). The first time my scripts access two specific objects I get the following:
MissingReferenceException: The object of type 'TrollAudioManager' 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.
I've verified that all the components and objects including the TrollAudioManager is being cleared and calling Start() again like normal (which does the re-grabbing logic as well).
To prevent this error I've added a null check and re-grab the component if it's missing. However, if I then run a Play on that component (basically just a layer over a standard Audio Source), it plays two of the same clip with a single call to Play().
So the issue I'm struggling with:
1) If I don't put a null check and re-grab the components I get a MissingReference (which technically should already be set from Start)
2) If I put in that null check/ grab; the audio plays twice as if there were two components of that type playing (I've verified there is only one after the reload)
I've also made sure to check that all my objects aren't static and these objects aren't being sent to DontDestroyOnLoad anywhere.
I know it's hard to determine without the classes themselves, but any idea on a cause or solution?
At first glance, I'd assume that you have some reference being carried between scenes (maybe a static field?). If that's not it, something might be destroying the object in your scene.
According to the error message, the component did exist at one point... it's just been destroyed, since then.
There is a static variable within that class however removing that doesn't fix the issue.
Actually even loading a different scene and then loading back into this one produces the same issue; so with my Hierarchy I can see the objects all get destroyed when the next scene loads and are recreated when the problem scene is loaded.
I do set some events within this code, could those references be seen as static?
Answer by Xymbiot3 · Jul 11, 2014 at 09:58 PM
I figured out the answer was due to setting events. I am using the 'Dialoguer' asset from the store and noticed the issue kept happening in the same event handler. After looking through the base code I found the function 'ClearAll()' which simply sets all the event handlers to null. Doing this before I re-set them releases those references to the old handlers and properly re-attaches the latest events.
I'm not great with event handlers but from what I gather they act almost like a pointer so simply trying to point them somewhere else without releasing what they point to currently was my issue.
yes, exactly that was happening to me. detaching the events from the handlers using -=, solved the problem.