DontDestroyOnLoad problem

Hi!

I have a problem in using of DontDestroyOnLoad method.
I have empty gameObject (SciptObject) with SomeScript in it. Script has public variable “Button”, this variable is filled in Unity editor (dragged button to an empty field in script properties in Inspector window). For this script I`m using DontDestroyOnLoad:

DontDestroyOnLoad(this.gameObject);

After some actions I`m loading another scene and the loading the first scene and one more ScriptObject is created with empty variable Button.

Is it posible to destroy new ScriptObject and use the first one?

Thanks!

What is the public variable “Button”? Is it another GameObject? If so, then that object is probably getting destroyed, and therefore, the original instance of your singleton object, which had a reference to the Button in the old level, now doesn’t have a reference to this new Button in this new version of the level.

If this is the case, you’ll need to find the object through script on Level Load, using Find, or Find by Tag, or something like that.