Search Unity

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:
    6,011
    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,223
    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,223
    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,223
    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,223
    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,223
    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.