The question is answered, right answer was accepted
TextAlignmentOptions is bugged if set via inspector (Unity 2020.3.2f1)
I am using Unity 2020.3.2f1 and TextMeshPro 3.0.6. I want to expose the TMPro TextAlignmentOptions to the inspector and assign it via code during runtime.
e.g.:
public TMP_Text tmpText;
public TextAlignmentOptions myAlignement;
private void Start()
{
tmpText.alignement = myAlignement;
}
The problem is that during runtime the text is always centered. I've debugged the code and noticed that the value of myAlignement is always 0. I know that enums can be converted to ints however the value of myAlignement always remains 0 no matter what I change, looking at the enum, its lowest int value is 257, so I can't even cast it back into TextAlignmentOptions if I wanted to.
I've already checked if it's a general problem with Alignements in my TMPro version, if I assign the alignment 'hard' in code like tmpText.alignement = TextAlignmentOptions.TopLeft; everything works. But since I want to be able to set the alignement in the inspector this isn't an option.
I already googled the problem but only found responses regarding how to convert HorizontalAlignmentOptions and VerticalAlignmentOptions to TextAlignmentOptions.
Answer by CedricLehr · Aug 01, 2021 at 10:51 PM
Apparently the problem was that the default value of an enum is not the first entry of it but 0, so if the enum in question doesn't have a valid entry for 0 it will create errors.
I would've noticed this sooner but in the inspector the displayed values were all correct, it was only causing problems during runtime.
I fixed it now by assigning my desired default value if the value of the enum is zero, however it still seems like quite the big oversight that this isn't handled by the TMPro editor code.
Follow this Question
Related Questions
Weird text field bug? 1 Answer
Need help with a Null Reference Exception Error 0 Answers
Text (in canvas) is not visible after build (and other bugs) 0 Answers
Mobile game lags when text mesh and gui text updating 0 Answers
TMPro Rendering issue. 0 Answers