Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

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

Discussion in 'Scripting' started by Kiupe, Feb 3, 2016.

Thread Status:
Not open for further replies.
  1. Kiupe

    Kiupe

    Joined:
    Feb 1, 2013
    Posts:
    528
    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 :)

    Thanks
     
    dangalg likes this.
  2. A.Killingbeck

    A.Killingbeck

    Joined:
    Feb 21, 2014
    Posts:
    483
    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"?
     
  3. Kiupe

    Kiupe

    Joined:
    Feb 1, 2013
    Posts:
    528
    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.
     
    dangalg, HorusVision and guneyozsan like this.
  4. UsmanAbbasi

    UsmanAbbasi

    Joined:
    Dec 29, 2015
    Posts:
    2
    This saved me. :)
    .
     
    dangalg likes this.
  5. HappySloth

    HappySloth

    Joined:
    Feb 27, 2014
    Posts:
    4
    Thanks A.Killingbeck, you saved me a lot of time :D
     
    dangalg likes this.
  6. huseyinbaba58

    huseyinbaba58

    Joined:
    Feb 12, 2020
    Posts:
    146
    I encountered this problem.But I can't fix it.Can you help me?
     
  7. wallabe

    wallabe

    Joined:
    Oct 6, 2017
    Posts:
    6
    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()
     
    Errandir and Buftler like this.
  8. joshuapeacock

    joshuapeacock

    Joined:
    Apr 12, 2018
    Posts:
    3
    This saved me. Thank you!
     
  9. rytalfo

    rytalfo

    Joined:
    Jan 15, 2019
    Posts:
    5
    you saved my butt
     
  10. LunaWoolf

    LunaWoolf

    Joined:
    Nov 9, 2018
    Posts:
    6
    You saved my ass. Thank you SO much
     
  11. krzpia

    krzpia

    Joined:
    May 23, 2020
    Posts:
    1
    WOW Thanks Joshua! It was difficult to understand for me!
     
Thread Status:
Not open for further replies.