UIToolkit having weird intermittent display issues

Hi, recently started using UIToolkit and been having issues with elements not displaying properly. I’ve been having the following issues in a build project

  • In my one scene the user clicks through a series of buttons and each time they do, some buttons will disappear and the next set is supposed to display. I achieve this by programmatically setting the style.display to None or Flex. When going back to this scene, sometimes the improper buttons will be displaying, or no buttons will display. It only seems to happen on random occasions
  • Sometimes elements such as photos will randomly take up the whole screen, or not be the correct size they normally are, I’ve even had some elements be upside down
  • Sometimes icons will have the incorrect background-image, they will use the same image as another icon somewhere else on the screen

These issues never seem to happen the first time I go onto a scene, and randomly happen when going to a scene a second or third time. I don’t use any inline styles. It almost seems like the Stylesheet, or random properties decide to just not apply.

It seems like you change the UI state upon scene transition:

  • Are you loading the scene additively?

  • How are you loading/saving the UI state when toggling the scenes?

  • When changing scene/destroying the game object are you cleaning up all your delegate?

  • Are you doing the setup of your UI in onEnable? Is the problem occurring in the editor or in builds also?

  • The scenes are all being loaded single
  • Each time I load the scene, I have been setting the UI states in OnEnable, although previously I had assumed that the UI states would go back to whatever was set in the stylesheet, which seemed the case sometimes but other times it was not
  • I am not using any delegate as far as I know
  • Originally UI setup was in Start() but I saw mention of doing UI setup in OnEnable in another thread so have so far changed one scene to use OnEnable, but it doesn’t seem to fix the problem

If you add elements manually directly to the panel root (not under the UI document root but the real top) they will survive or if you keep a reference to the visual element (in a static variable of on another scene object) then it is possible that both the old and new UI is alive and that would be special.

It is hard to pinpoint the issue without more context so I will suggest a few pointers and then I would suggest filling a bug including your project so somebody can look at the problem. Of course, the more you trim your repro project the more we find the problem quickly so it is well appreciated if you take the time to trim down the repro project to a minimal state and provide clear reproduction steps.

New pointers:

  • By using different panelSettings between scenes, if no UI Document is alive and point to the panelSetting the panel will be destroyed. If this solve your problem the problem could be a leftover on the root of the panel
  • Check static variables

I am running out of ideas! :eyes: