After upgrading my project to Unity 5.1, some of my UI elements have begun to inexplicably flicker and disappear at random. I have scoured the change log looking for a possible explanation but I cannot find any potential culprits. The best way I can explain the problem is with these two short videos:
Working 5.0 UI
Broken 5.1 UI
I have rows of “action buttons” that are made up of various Unity UI elements, but they are primarily just basic buttons with an image component. Each button is instantiated from the same button prefab and they are assigned different sprites to use at runtime. Each button registers itself with an event from another class that will inform it when it needs to perform its update/cooldown routine.
The bulk of the button-updating logic is handled in this script. That script is the only location in the entire project where the sprites are enabled or disabled during runtime. I added two simple debugging messages in the methods that hide the UI elements, but that code is not being reached when the UI is flickering on/off. This leads me to believe that it is a bug on Unity’s end but I’m not sure what more information I can provide to help diagnose it.
The code and logic for handling these buttons was completely unchanged when upgrading from version 5.0 to 5.1. And as you can see in the second video, the UI elements will disappear seemingly randomly when just moving the camera around. Perhaps most perplexing is that only some of the buttons will be affected when they are all instantiated from the same prefab.
I realize I’m not providing much information to work with, but I’m simply not sure what I can do to try and diagnose or fix this problem. If anyone has any advice to help debug this further, or is aware of this being a known bug, I would greatly appreciate any help.
After giving it some more thought, I think this might be related to two Draggable and Droppable scripts I have attached to each button. When viewing the button prefab in the inspector, the check box that usually allows me to enable/disable scripts is not present. The scripts are not generating any compiler warnings or errors though. Is there any reason that the behavior of these scripts may have become broken in this patch?
My scripts for these buttons are so highly coupled with others in the project that it would be difficult to test it completely by itself.
I did test it using a button prefab without the Draggable and Droppable scripts mentioned above, but the problem still occurs. I’m going to go ahead and submit a bug report and reference this thread. I’ll submit my entire project if it’s not too large so that they can take a look at it in action.
When some of the icons disappear, can you pause the game and try moving the “invisible” icon around to make sure it’s not rendered underneath its background? I believe your issue is caused by broken UI depth sorting. There were several reports already, people experiencing a similar issue:
Thank you for your reply Bennet. I have downloaded 1p2 and I still have a flickering issue. I took a video and made a gif of it which I hope is attached correctly; perhaps this is a different issue to the one you were having then?
What I am doing here is Canvas1.gameobject.setActive(false) and Canvas2.gameobject.SetActive(true) at the same time. It looks like it does some pretty weird stuff!
Canvas1 is basically a ‘landing’ to other menus. Canvas2 is a bunch of InputFields. It looks like Canvas1 is faded off, then on, then off again as Canvas2 is then shown.
I had this problem with a custom editor window and dynamic textures. I refactored to just pre-allocate the textures into a texture array instead of reusing one texture over and over, and the flickering went away. I was trying to save memory, but oh well.