DontDestroyOnLoad Object Reference Goes Missing After Scene Change

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?

3 Likes

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:

static SCRIPTNAME instance;

void Awake() {
    //Singleton method
    if (instance == null) {
        //First run, set the instance
        instance = this;
        DontDestroyOnLoad(gameObject);

    } else if (instance != this) {
        //Instance is not the same as the one we have, destroy old one, and reset to newest one
        Destroy(instance.gameObject);
        instance = this;
        DontDestroyOnLoad(gameObject);
    }
}
16 Likes

thank you so much it worked for me

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.

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.

I agree with the other poster. Deserves a million likes. Thank you very much for that!

Amazing! It worked for me as well! Thanks a lot.

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?

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:

public void DestroyThyself()
{
   Destroy(gameObject);
   Instance = null;    // because destroy doesn't happen until end of frame
}

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

Thank you bro! You saved my ass

this single handedly saved my project. I had a dialoguemanager that was removing all of it’s references and copying itself on a scene change. Then on any future trigger it would try to call the old object without references and would get an error. THIS saved EVERYTHING. Thank you 1000 times!!!

Please use the “Like” button instead of necroing threads.

Thanks.

Thread Closed.