In several places I use LayoutUtility to help dynamically set the size of some UI elements. This occasionally causes this error:
01:08:05 InvalidOperationException: HashSet have been modified while it was iterated over
01:08:05 at System.Collections.Generic.HashSet`1+Enumerator[UnityEngine.UI.Text].CheckState () [0x00000] in <filename unknown>:0
01:08:05 at System.Collections.Generic.HashSet`1+Enumerator[UnityEngine.UI.Text].MoveNext () [0x00000] in <filename unknown>:0
01:08:05 at UnityEngine.UI.FontUpdateTracker.RebuildForFont (UnityEngine.Font f) [0x00000] in <filename unknown>:0
01:08:05 at UnityEngine.Font.InvokeTextureRebuilt_Internal (UnityEngine.Font font) [0x00000] in <filename unknown>:0
01:08:05 UnityEngine.TextGenerator:INTERNAL_CALL_Populate_Internal_cpp(TextGenerator, String, Font, Color&, Int32, Single, Single, FontStyle, Boolean,
01:08:05 Boolean, Int32, Int32, Int32, Int32, Boolean, TextAnchor, Single, Single, Single, Single, Boolean, Boolean, UInt32&)
01:08:05 UnityEngine.TextGenerator:Populate_Internal_cpp(String, Font, Color, Int32, Single, Single, FontStyle, Boolean, Boolean, Int32, Int32, Int32,
01:08:05 Int32, Boolean, TextAnchor, Single, Single, Single, Single, Boolean, Boolean, UInt32&)
01:08:05 UnityEngine.TextGenerator:Populate_Internal(String, Font, Color, Int32, Single, Single, FontStyle, Boolean, Boolean, Int32, Int32, VerticalWra
01:08:05 pMode, HorizontalWrapMode, Boolean, TextAnchor, Vector2, Vector2, Boolean, Boolean, TextGenerationError&)
01:08:05 UnityEngine.TextGenerator:PopulateAlways(String, TextGenerationSettings)
01:08:05 UnityEngine.TextGenerator:PopulateWithError(String, TextGenerationSettings)
01:08:05 UnityEngine.TextGenerator:Populate(String, TextGenerationSettings)
01:08:05 UnityEngine.TextGenerator:GetPreferredHeight(String, TextGenerationSettings)
01:08:05 UnityEngine.UI.Text:get_preferredHeight()
01:08:05 UnityEngine.UI.LayoutUtility:<GetPreferredHeight>m__6(ILayoutElement)
01:08:05 UnityEngine.UI.LayoutUtility:GetLayoutProperty(RectTransform, Func`2, Single, ILayoutElement&)
01:08:05 UnityEngine.UI.LayoutUtility:GetLayoutProperty(RectTransform, Func`2, Single)
01:08:05 UnityEngine.UI.LayoutUtility:GetPreferredHeight(RectTransform)
Once this occurs, we wind up with a lot of garbled text in the game, which only goes away when code changes the text or other property of a Text object, forcing it to redraw. Unfortunately, we have a lot of Text in the game that code never touches, so we can wind up with garbled text all over that never goes away.
Is this a known issue or is there some way to prevent this?
I’m running Unity 5.5.0p2