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

MissingReferenceException when scene is reloaded

Discussion in 'Editor & General Support' started by adri1992, May 29, 2018.

  1. adri1992

    adri1992

    Joined:
    Dec 14, 2015
    Posts:
    34
    Hi guys!

    I've read a lot of answers about this topic but I'm not able to solve this problem... I'm going crazy :(

    Context:

    - I've a game screen, with some 2D objects and some UI Canvas elements.
    - When the game finishes, appears a new screen (without any element of the previous scene.. It's a "clean" scene).
    - In this last scene, there is a button for game screen return.
    - When the user clicks the game screen appears but when it should access to some elements, the app crashes with this error message

    "MissingReferenceException: The object of type 'GameObject' 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."

    Any idea? The first time always is working fine, but it crashes in the "second round". How can I solve that? Can I load the game screen always as the first time?
     
  2. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Does the error mention a script and line number?
     
  3. adri1992

    adri1992

    Joined:
    Dec 14, 2015
    Posts:
    34
    Yes. The crash occurs in this line:

    "sliderMapPlayer.updateSlider (targetCell);"

    sliderMapPlayer (Slider canvas element), in the second round is null, so when I invoke "updateSlider" it crashes. However, in the hierarchy, seems that is correctly attached to a GameObject... I have no idea that what can be occuring
     
  4. adri1992

    adri1992

    Joined:
    Dec 14, 2015
    Posts:
    34
    any help? =(
     
  5. FMark92

    FMark92

    Joined:
    May 18, 2017
    Posts:
    1,243
    Hierarchy does not have a bearing on anything other than local transform and maybe, if you implemented it in any capacity, child search.
    Did you connect references in editor?
     
  6. adri1992

    adri1992

    Joined:
    Dec 14, 2015
    Posts:
    34
    Yes, all references are connected in editor
     
  7. FMark92

    FMark92

    Joined:
    May 18, 2017
    Posts:
    1,243
    What do you do with the old scene? Are you switching scenes and, in doing so, destroying GameObjects which you're then trying to access?
     
  8. adri1992

    adri1992

    Joined:
    Dec 14, 2015
    Posts:
    34
    No, I'm not destroying any GameObject
     
  9. FMark92

    FMark92

    Joined:
    May 18, 2017
    Posts:
    1,243
    So you're loading the scene in additive mode?
     
  10. adri1992

    adri1992

    Joined:
    Dec 14, 2015
    Posts:
    34
    I'm loading all scenes by default (I guess that it's single mode). Should I try through additive mode? why?
    Thanks so much for your help!
     
  11. FMark92

    FMark92

    Joined:
    May 18, 2017
    Posts:
    1,243
    So you ARE destroying GameObjects. If you don't load next scene in additive mode, previous scene gets destroyed. And guess what scenes contain;
    GameObjects.
     
    adri1992 likes this.
  12. adri1992

    adri1992

    Joined:
    Dec 14, 2015
    Posts:
    34
    I was refering to destroy GameObjects through code, because from what I know, when the scene that crashes is recreated, the GameObjects should be recreated and so the references reconnected in the editor, no?
     
    Last edited: Jun 5, 2018
  13. FMark92

    FMark92

    Joined:
    May 18, 2017
    Posts:
    1,243
    True.
    Are you doing anything in Awake functions?
     
  14. adri1992

    adri1992

    Joined:
    Dec 14, 2015
    Posts:
    34
    No, I'm not using any Awake function.
     
  15. adri1992

    adri1992

    Joined:
    Dec 14, 2015
    Posts:
    34
    If I check null condition on the first "GameObject destroyed", the same problem occurs with other game objects, but I can't get any solution...
    All of them are in the hierarchy, and all of them are associate through the editor. Only when one of that game objects do an action, the app crashes.
     
  16. verfrn

    verfrn

    Joined:
    May 3, 2018
    Posts:
    1
    Agree with comment:
    I'm using
    Code (CSharp):
    1. SceneManager.LoadScene(param, LoadSceneMode.Single)
    .
    I'm using same script attached to a cloned object that is present in 2 different scenes. When the method of the script, in second scene, refers to itself (transform.<property>) in order to add a list of children, throw the exception 'MissingReferenceException: The object of type 'GameObject' 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'm searching for a solution or alternative, but reusing the same script.
     
    Last edited: Oct 11, 2018
    Vukiz likes this.
  17. gaming_sharechat

    gaming_sharechat

    Joined:
    Nov 13, 2019
    Posts:
    2
    Any luck here. Even I facing a similar issue.
    The scene and all objects work fine when I load the scene the first time.
    When I close that level, returns to the menu scene and load the level again,
    it gives me the below error.

    MissingReferenceException: The object of type 'GameObject' 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.
     
    TroubletimeQAQ and Roger-W like this.
  18. Zenbax

    Zenbax

    Joined:
    Mar 5, 2018
    Posts:
    1
    Got the same problem, annoying af. Can't find any solutions anywhere..
     
    rukomusic likes this.
  19. venkcauk

    venkcauk

    Joined:
    Jan 16, 2021
    Posts:
    1
    no soln ?
     
  20. babanty

    babanty

    Joined:
    Dec 4, 2020
    Posts:
    4
    Got the same problem. Unity vers. 2019.4.17f1
     
  21. babanty

    babanty

    Joined:
    Dec 4, 2020
    Posts:
    4
    I decided. In my case, there is problem with subscribing to a static event. When the scene is destroyed, the subscribing is not destroyed.
     
    Abulero, lucdima, ozkntn and 8 others like this.
  22. Laurieray

    Laurieray

    Joined:
    Nov 9, 2017
    Posts:
    2
    I was getting the same exception a lot (spamming over and over), until I added the OnHierarchy() event and refreshed the reference, same as I was already doing in OnEnable().

    I still get two errors (no longer spamming) whenever I click Play or Stop, but my custom editor window no longer breaks.
     
  23. Laurieray

    Laurieray

    Joined:
    Nov 9, 2017
    Posts:
    2
    ... and the reason I was still getting those two errors? I was iterating over a List<>. Even though I had "null-checked" the list itself, the error was being thrown on each of the list items, which were actually null.

    So, the solution? "Null-check" each item in the list (inside of the for loop).
     
  24. notops3

    notops3

    Joined:
    Oct 25, 2021
    Posts:
    1
    Thanks you a lot. I created OnDestroy function where i unsubscribe event and its worked.
     
    Vmpwje and ivlevfima like this.
  25. Eleocraft

    Eleocraft

    Joined:
    Jun 19, 2019
    Posts:
    17
    Sorry, but how exactly did you do this?

    Thing is, I have the same problem exept it is with [SerializeField] gameObject references. But I only get the error. the logic works, what proves that it is a bug
     
    Last edited: Jan 18, 2022
  26. AdamBebko

    AdamBebko

    Joined:
    Apr 8, 2016
    Posts:
    164
    same error here. very confusing to debug. On my end it's a call to

    if (audioSource == null) audioSource = GetComponent<AudioSource>(); // fails on second time scene loaded for some reason.

    I'm using additive scene loading, and double checked that scene is unloaded in between


    Edit: Just to expand on things, I was indeed able to solve it by ensuring that script unsubscribed from all events. But the error lines had nothing at all to do with any of the events.

    I suspect what's happening here is when the scene unloads, it notices the leftover event subscriptions, and says hey I gotta keep that those references around, so even though unity destroyed the game object, it didn't actually break those references. And that script happens to also be referencing the audio source, so maybe it keeps that reference around too. Then when the scene reloads there's some conflict with the two audio sources, since there's still a reference left over from before. You would expect a scene unload to clear all that out though..
     
    Last edited: Mar 10, 2022
  27. Tyc1Up

    Tyc1Up

    Joined:
    Jan 27, 2021
    Posts:
    2
    Newb here. This error was also happening for me whenever I used
    Code (CSharp):
    1. EditorSceneManager.LoadScene(0)
    to "Restart" my test level. But it was happening ONLY to one specific powerup. Everything else worked as intended.
    Seems this was happening due to a Delegate method not being unsubscribed from the event when the game object was destroyed. To clarify with an example:
    We have the delegate in the "PowerUp" script:
    Code (CSharp):
    1. public delegate void SpeedCollected();
    2. public static event SpeedCollected OnSpeedCollect;
    3. private void OnTriggerEnter2D(Collider2D other)
    4.     {
    5.         if (other.CompareTag("Player"))
    6.         {
    7.             switch(_powerUPId)
    8.             {
    9.              
    10.                 case 1:
    11.                     OnSpeedCollect?.Invoke();
    12.                     break;
    13.             }
    14.             Destroy(this.gameObject);
    15.         }
    16.  
    Then we have the subscriber with a method:
    Code (CSharp):
    1. void OnEnable()
    2.     {
    3. PowerUP.OnSpeedCollect += EnableSpeedPowerUP;
    4.     }
    5.  
    6. void EnableSpeedPowerUP()
    7. {
    8. //do wtv
    9. }
    This error was happening because in the subscriber script, you need to have:
    Code (CSharp):
    1. private void OnDisable()
    2.     {
    3. PowerUP.OnSpeedCollect -= EnableSpeedPowerUP;
    4. }
    Else, when you try to send the event after restarting the scene, your event script will try to access the destroyed, still subscribed event.
    Pretty sure not everyone has the same apparent issue but the root cause is the same. A script trying to access a destroyed script, instead of the freshly created new one.
    Also again, newb here so if this does not help, I got nothing else ...
     
    asgnchrv likes this.
  28. CheMBurN

    CheMBurN

    Joined:
    Dec 13, 2020
    Posts:
    20
    Whelp this was my problem.
     
    kjl987 likes this.
  29. NoobGeek

    NoobGeek

    Joined:
    Mar 2, 2021
    Posts:
    2
    For myself, I have identified 2 reasons why this could happen:
    1. There is no unsubscription from the event (unsubscribe in OnDisable)
    2. The event is declared as static (just remove the static keyword and subscribe to the event through an instance of the class).
     
  30. lucdima

    lucdima

    Joined:
    Jul 7, 2023
    Posts:
    1
    Thank you to all the comments about subscribed event!
    For me this solve it!!

    Code (CSharp):
    1.     private void OnDestroy()
    2.     {
    3.         CardController.onCardClicked -= CardClicked;
    4.     }
    5.