TextMesh Pro UGUI hidden characters?

I noticed the content pulled from a seemingly empty TMP UGUI Text object was not hitting my string.IsNullOrEmpty conditional. When I printed it to Debug it was empty, but when I got the length of it, it was 1. Is there a hidden character (new line, tab, space) that is automatically given to all TMP Text objects?

Unity doesn’t use the version of .NET that includes string.IsNullOrWhitespace, so a text object not being equal to empty when you think it should creates unnecessary calls to Trim().

Can you provide a simple script example the reproduces the behavior you are running into?

Stephan,

Simply creating a script with a TMP_InputField class variable, then assigning a reference to that variable from the editor will do it. Now in the script when you reference the text of that input field (like: emailInputField.text), the length will be 1 even though it’s empty (it’s populated with some whitespace character that can’t be seen). Calling trim on it (emailInputField.text.Trim()) solves the issue, so it must be like a newline, space, etc.

It seems like this was brought up back in April by someone else as well (Unity Issue Tracker - When there is no text in the TextMesh Pro Input Field, the length of it is 1). They tested on older versions of Unity but I can confirm the issue still arises on Unity 2017.2.0f3.

EDIT: Note that if you reference the underlying TextMeshProUGUI object of the InputField directly, the same issue occurs.

When working with the TMP_InputField, you should be accessing the TMP_InputField.text property and not the text property of the underlying text component which is only there to display text. The length of the text property of the input field itself should be correct.

You should be using the following.

string text = tmp_inputField.text;

Why not rely / use the .text property of the underlying text component?

Let’s say the TMP_InputField is set to Content Type = Password. The underlying text component would contain a bunch of “******” whereas the TMP_InputField.text property would contain the actual password.

Here is a [previous post](http://var text = tmp_inputField.textComponent.text;) about this topic.

5 Likes

Stephan, my original response WAS using the text property of the InputField, I stated that multiple times (“like: emailInputField.text”, exact quote from above). The edit I added at the bottom was just letting you know this also occurs if you use the underlying Text object. But using the TMP InputField definitely does still set the string length to 1 and not 0 for an empty input field.

I ran several tests and the string length is returning zero on my end when I delete the text.

Can you provide a simple repro project or script that would enable me to reproduce this behavior?

Hi @Stephan-B ,

I have run into something similar to what @kabumere describes. However, when I was in the process of doing a small repro for you. When testing the repro, everything worked as expected and as you described. Which now made me wonder if I am doing something wrong or I am not able to reproduce the problem I am experiencing.

In my case, I have a Base Scene which loads a UI Scene. In this UI Scene there are several Canvases, which contain Child Canvases and CanvasGroups. I enable the Canvases and CanvasGroups as I required them. However, on my main project whenever I enable the Canvas that contains the Input Fields, they are empty but do not show the Placeholder Text. On the other hand, when I did the same thing on the repro, the Input Fields are not empty and display the Placeholder Text. As I said, if you try this, it will work as I just checked on my test repro. However, still does not work on my main project. :frowning:

After some trial and error, I found a workaround for my specific case (still unsure why happens in the first place). Just after enabling the Canvas and CanvasGroup of my InputFields TMP, I quickly assign some dummy string and then remove it. I tried doing this before enabling the Canvas and CanvasGroup, but it did not work.

_inputField.text = “a”;
_inputField.text = “”;

This is not ideal by all means, but it does the trick for me.

So the issue you are running into is the placeholder text not showing up when you enable these canvases?

Yes. The Input Fields appear “empty” and do not display the Placeholder Text they are supposed to do when their text property contains nothing.

However, when I tried this on a repro project, it worked as expected and I was not able to reproduce the same situation I get on my main project.

By their text property, you mean the TMP_InputField.text and not the child text object’s text property?

In terms of reproduction, are you using the same version of Unity in both cases? Same number of Canvases?

Yes, I am using TMP_InputField.text property, not the textComponent.text.

Yes, I am using the same version of Unity, 2017.1.1f1 (64 bit) OSX.

Yes, I tried (to the best of my knowledge) to reproduce it using the same number of Canvases and Elements, loading the InputField Scene from a script from another Scene, etc. without success.

Is there any extra information I can provide? I will give it another try tomorrow and see if I can repro it. If I am successful (I hope I am) I will send you a repro.

If it only happens in your project, you could provide me with a Repro of the project either via some link in a Private Message or via the Unity bug reporter and then providing me the case #.

I am experiencing this issue myself. Right now a work around is to trim out the zero space character from the string as the OP’s work around didn’t work for me. Would normally be fine only it broke byte parsing when checking for a valid IP address.

string text = inputField.text.Trim((char)8203);

There should not be any extra character in the TMP_InputField.text property. On the child text object there would be but not on the parent.

Can you check to make sure we are talking about the same object and property?

17 Likes

I ran into the same issue, thanks Chris, your workaround worked for me… Still, I agree, there shouldn’t be any extra character in the TMP_InputField.text property… Running on Unity 2018.1.0f2

Same problem for me running Unity 2018.1.7f1
Thanks for the workaround Chris.

I just tested this again and the TMP_InputField.text does not contain any additional characters. The child / underlying text object .text property would but this child should not be accessed.

The parent TMP_InputField.text should.

If anyone can provide an example / script that would enable me to reproduce this, that would be most useful.

I would say this is the cause of the problem, accessing the child is the wrong way to do it. Accessing the input field itself works fine and no work around’s needed.

1 Like

I’m assuming that this is why the child text of the input field will not equal a string of the same value? Just ran into this and was trying to see if someone else had the same problem. The fix is to just access the Input Field instead of the child text. If this is unrelated, then potentially a new bug?

The child text object should never be accessed directly as it contains formatting characters in addition to the characters that will be displayed which will never matches the content of the parent TMP_InputField.text.

For example, in the case of a password, the child text object .text property would contain something like “****” whereas the parent TMP_InputField.text would contain “1234”.

1 Like

I was having the same problem, Stephen_B your explanation has been very helpful and was exactly what I wasn’t doing.