Sometimes when developing, we run into bugs. Here, one of our programmers details how we approached a bug that was occurring with our text input in the Clinical Encounters platform.
For being a unity project, ours is heavily-centered around Canvas and UI work instead of things like 3D or 2D game work. Unity’s built-in text component works pretty well for the most part, but we encountered a strange problem with it. In the editor, every now and again, all text across the program would suddenly transform into a jumbled mess. The program still ran just fine, but with the text unreadable, it was practically unusable. We thought it was simply an editor-only problem, so we ignored it and went on our merry ways since it wasn’t affecting the builds.
Or so we thought.
Not Just an Editor Problem
During some internal testing, somebody had encountered the bug while using a built version of the software. For reference, normal text (Left Rendering) would become jumbled (Right Rendering). Now that we had evidence that the bug could happen in a build, we started to take it seriously.
The first step was to research why something like this was happening. One of the lead programmers on the Clinical Encounters team discovered that it seemed that the reference to the font we used was being updated so it was seemingly outside of an array of given fonts. This would explain why the text was jumbled, but not what caused the de-reference or how to prevent it, so we kept going.
TextMesh Pro was also discovered as an alternative text component. TextMesh Pro is a unity plugin that was picked up by the Unity team a year ago during the reign of Unity 5.5. It has a range of benefits over Unity’s default text and would overall be an upgrade to the text we were using, but the downside was that we would have to replace all of the text across the entire software. This includes replacing the assets, reformatting them, and making sure they look as good as they did before. This would be a big, tedious commitment, so we opted to search for other options unless it was necessary to make the switch.
It seemed that the implemented try/catch wasn’t working and the error kept slipping through.
As time went on, we would occasionally encounter the bug here and there. It seemed that the implemented try/catch wasn’t working and the error kept slipping through, so I iterated on the try/catch implementation every time the bug surfaced and we kept on trucking.
Upon looking into it on the code side of things, I discovered that it would happen when a custom script helped our input fields resize as appropriate. Since Unity was showing an error when this would happen I threw the code in a try/catch to see if I could stop this from happening. Unfortunately, none of us knew how to recreate the bug on command, so we simply had to wait to see if it was fixed.
Eventually, we made note of how the bug seemed to happen mostly when Unity lost and regained focus. Before this, the build would pause if the executable window lost focus and would resume when clicked on again. So, we changed the program to run in the background. This definitely reduced the frequency of the bug, but it wasn’t completely solved.
Finally, after a certain iteration of the try/catch and adjusting the line that was throwing the error, I got the catch to work and the error was avoided! I can’t say for sure exactly what the root of the cause was, but it involved referencing the size of the input field and then calculating the new height with that information all on the same line. Separating the current size calculation and when we calculated the new size worked and we haven’t had the bug since.
Thanks to the discovery of TextMesh Pro, we will be heavily considering it for future projects. We’ve even used it in one place in Clinical Encounters already and it’s been pretty helpful for that. It has some quirks still, but overall it’s a great asset to know about. Having options can be important and can add great flexibility to your development cycle. As for the text issue, we will hopefully never have to encounter something like that again, but we have the tools we will need to handle it if it ever comes up.
You can read more about the Clinical Encounters platform at its website.