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

DontDestroyOnLoad Object Reference Goes Missing After Scene Change

Discussion in 'Scripting' started by Dawzy, Dec 16, 2019.

  1. Dawzy

    Dawzy

    Joined:
    Jul 20, 2019
    Posts:
    4
    I have a button with a function in onClick and onPointerEnter. The function belongs to a game object, which I use DontDestroyOnLoad() on, so I can use it between scenes.

    Problem is that when I switch scenes, then go back to the original scene, the reference for the game object that was there in onClick, and onPointerEvent, goes missing, even though the object was never destroyed in the first place, just jumped between scenes.

    My question is, how can you maintain a reference to an object, that has DontDestroyOnLoad() on its instance?
     
    Thincc, thilak02 and DragonAngel1st like this.
  2. Dawzy

    Dawzy

    Joined:
    Jul 20, 2019
    Posts:
    4
    I got around it, by just making the singleton change to the newest one.
    In my specific case this works, but destroying the old singleton and resetting a new one might not work for everyone.

    Code I used:
    Code (CSharp):
    1. static SCRIPTNAME instance;
    2.  
    3. void Awake() {
    4.     //Singleton method
    5.     if (instance == null) {
    6.         //First run, set the instance
    7.         instance = this;
    8.         DontDestroyOnLoad(gameObject);
    9.  
    10.     } else if (instance != this) {
    11.         //Instance is not the same as the one we have, destroy old one, and reset to newest one
    12.         Destroy(instance.gameObject);
    13.         instance = this;
    14.         DontDestroyOnLoad(gameObject);
    15.     }
    16. }
     
  3. Ensar_DAS

    Ensar_DAS

    Joined:
    May 3, 2020
    Posts:
    1
    thank you so much it worked for me
     
  4. MichealT

    MichealT

    Joined:
    Aug 28, 2019
    Posts:
    1

    This deserves a million likes! I searched everywhere trying to figure out how to keep all my references, this is the only one that would work!! Thank you.
     
  5. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,316
    Please note that this post isn't related to 2D features so for the future, it's best to post scripting questions to that forum here.

    Thanks.
     
  6. jacko_the_gog

    jacko_the_gog

    Joined:
    Mar 15, 2020
    Posts:
    1
    I agree with the other poster. Deserves a million likes. Thank you very much for that!
     
  7. Thrasher96x

    Thrasher96x

    Joined:
    Oct 15, 2020
    Posts:
    2
    Amazing! It worked for me as well! Thanks a lot.
     
  8. Wildhie

    Wildhie

    Joined:
    Jul 4, 2022
    Posts:
    6
    It works on the problem with the references to functions from other gameobjects.
    But in my case I got alots of variables that I want to save too.
    For example, in my home scene I got talents for 27 different "stats" that I save in that scenes manager. Also a enemyindex between 0 and 91 that determines how the fight mechanics.
    I still want to keep all my veriables saved even if I change scene. Dontdestroyonload will do that. But when we overrides the old manager with this to keep references to functions, then I lose all my saved variables.

    I can transfer it with this.monsterindex = manager.instance.monsterindex in awake before I destroy the old object. But that will make my awake 200lines long.
    I feel there's gotta be a better way?

    Make an abstract singelton class that contains all my variables mabye?
     
  9. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,514
    There is. But please refrain from necro-posting to old threads and instead make your own... it's FREE!

    How to report your problem productively in the Unity3D forums:

    http://plbm.com/?p=220

    This is the bare minimum of information to report:

    - what you want
    - what you tried
    - what you expected to happen
    - what actually happened, especially any errors you see
    - links to documentation you used to cross-check your work (CRITICAL!!!)

    The actual problem in this thread can be best thought of it as an object lifecycle issue.

    Identify the range of time you want each object to live.

    If it must exceed a single frame load, make it DDOL.

    What you describe sounds sort of like a player database, so probably:
    - create the object at app start (or player login) and NEVER remove it, NEVER make another one
    - before the app exits, save all you want to save

    If it's something that lives for X frames, then goes away, like a GameManager, then make it go away when you don't want it anymore.

    ULTRA-simple static solution to a GameManager:

    https://forum.unity.com/threads/i-need-to-save-the-score-when-the-scene-resets.1168766/#post-7488068

    https://gist.github.com/kurtdekker/50faa0d78cd978375b2fe465d55b282b

    OR for a more-complex "lives as a MonoBehaviour" solution...

    Simple Singleton (UnitySingleton):

    Some super-simple Singleton examples to take and modify:

    Simple Unity3D Singleton (no predefined data):

    https://gist.github.com/kurtdekker/775bb97614047072f7004d6fb9ccce30

    Unity3D Singleton with a Prefab (or a ScriptableObject) used for predefined data:

    https://gist.github.com/kurtdekker/2f07be6f6a844cf82110fc42a774a625

    These are pure-code solutions, do not put anything into any scene, just access it via .Instance!

    If it is a GameManager, when the game is over, make a function in that singleton that Destroys itself so the next time you access it you get a fresh one, something like:

    Code (csharp):
    1. public void DestroyThyself()
    2. {
    3.    Destroy(gameObject);
    4.    Instance = null;    // because destroy doesn't happen until end of frame
    5. }
    There are also lots of Youtube tutorials on the concepts involved in making a suitable GameManager, which obviously depends a lot on what your game might need.

    And if you really insist on barebones C# singleton, here's a Highlander:

    https://gist.github.com/kurtdekker/b860fe6734583f8dc70eec475b1e7163
     
  10. wemadehw

    wemadehw

    Joined:
    Apr 9, 2020
    Posts:
    3
    Thank you bro! You saved my ass