I have DoNotDestroyOnLoad(this.gameObject); on my canvas and when I load into a new scene the buttons on my UI don’t work anymore.
For example, the health still drains but the buttons and sliders don’t work anymore?
I have DoNotDestroyOnLoad(this.gameObject); on my canvas and when I load into a new scene the buttons on my UI don’t work anymore.
For example, the health still drains but the buttons and sliders don’t work anymore?
This is because the OnClick event bindings get reset. If you Pause the game and inspect the buttons, your OnClick events would probaby say something like,
"Missing "
The way I have solved UI through multiple scenes is make an empty game object, put objects that have UI - related scripts in them in it, and put ALL UI stuff in it (Pause, Inventory and HUD screens and their cameras).
Then make a prefab out of the root GameObject, call it “LevelUIPackage” or something.
If all the OnClick (or other) events are bound to a script that is within the same prefab (within the same root object), they will get preserved. Otherwise if they’re bound to a gameObject that’s outside the prefab, they will get lost between scenes.
Another advice: Don’t use DontDestroyOnLoad for UI, once you have your prefab UI package, just put it in every scene you need it in, and call it a day. That way you can select the scenes you want the UI in. It will save you the trouble if you quit the game to Main Menu for example.
make sure you are also destroying the object you put on the donotdestroy list when you reload your application or level. I had that problem signing out was trying you to a loginscreen but object were staying alive and on next login it was loaded twice on top of each other. I was also the victim of forgetting the event listener :O)
In my case, I made UIManager and include all necessary Text for Score counting.
When I declared its variable in GameManager and the game is resetting, the text doesn’t display anything.
I was wrong. I should declare its variable in UIManager, the first place its object attached to.
Now my score text display in any level I want. But of course I need to copy canvas and its elements to all scenes. I don’t make UIManager having Don’tDestroyOnLoad().