Missing reference after reloading a scene : but the referencing object still exist !!!

Hi guys,

After reloading a scene I have an error when one of my script tries to call a method on a public property it has which is another script inside another game object in the scene. This public property is set by simply drag & drop the game object which contains the target script directly in the editor.

I reload the scene like this : SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex,LoadSceneMode.Single);

When the error occurs I can see that :

  • the public property it’s not null, it’s still references the script in the game object in the scene
  • the game object in the scene still exists and the targeting script too

The error message is :
MissingReferenceException: The object of type ‘Rules’ 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 do not manually destroy the reference neither the game object which contains the targeting script. Moreover, when I reload the scene I expect everything to get back to their default state and for what I can see it’s the case. So I don’t understand why I’ve got this error.

Any help is welcomed :slight_smile:

Thanks

1 Like

Are you unsubscribing from all global events? Are you passing references of scene objects to scriptable objects/any other .asset file? Do you have any singletons which are not set to “DontDestroyOnLoad”?

10 Likes

Hi,

You’re right - I just noticed that I didn’t unsubscribe to some global events that are store in a static class - that was the cause of the error.

Thanks.

3 Likes

This saved me. :slight_smile:

.

1 Like

Thanks A.Killingbeck, you saved me a lot of time :smile:

1 Like

I encountered this problem.But I can’t fix it.Can you help me?

To anyone who might also have this issue check this answer. The anomalies you’re experiencing are probably coming from events referencing the old version of an object. Make sure events are loaded ThisEvent += ThisEvent_Fire in OnEnable() preferably and unloaded ThisEvent -= ThisEvent_Fire in OnDisable()

2 Likes

This saved me. Thank you!

you saved my butt

You saved my ass. Thank you SO much

WOW Thanks Joshua! It was difficult to understand for me!