- Home /
When loading into a new scene my UI doesn'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?
Answer by Eudaimonium · Jun 16, 2015 at 12:47 PM
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.
I read the first sentence and realise I forgot to put an eventsystem with it :x
Thanks for the tips and your answer!
I had the same thing and I was so confused on what was happening. :)
One of those errors you make when you just been going for too long in one session!
Same happend to me .... eventsystem solved problem - thanks!!
@Jan_Julius @BEFaughnan @buqal
How did adding the event system solve this? I have an event system and am still getting this problem. Any help would be appreciated!
Thanks
Answer by Unplug · Aug 22, 2017 at 05:11 PM
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)
Answer by gOzaru84 · Jan 04, 2019 at 08:41 AM
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().