Changing TMP_InputField text via script

I’m having a weird issue with the TMP InputField. If I change the text via script, e.g.:

nameField.text = "New Solution";

Then the text gets shifted left a bit:

As soon as I click the input field it seems to recalculate the position and the positioning gets fixed. Is there some method I need to call to force this recalculation?

Is this with the latest releases of TMP which are version 1.4.1 for Unity 2018.3 / 2018.4 and 2.0.1 for Unity 2019.x?

Yeah, this is on 2.0.1 for Unity 2019.1.8f1.

Just tested on my end and it appears to be working fine.

See if you get the same results creating a new TMP_InputField without modifying any of the positions / offsets of the child objects. Perhaps in your example, you modified these values which results in the incorrect positioning?

Okay yeah it doesn’t happen with a fresh TMP_InputField, but once I change the anchors of the input field itself (not the children) the problem shows up. In particular, I’m using MinX = 0, MaxX = MinY = MaxY = 1.

That said, I haven’t been able to reproduce it in an empty project yet, so I’ll keep fiddling around.

(In the empty project I noticed another issue though: the Text child of the InputField was using the default TMP font settings, which made it way too big for the input field. This only happened on the first input field though, which triggered the import of TMP Essentials.)

1 Like

I have noticed weirdness when creating / using a TMP object that was created prior to importing the TMP Essential Resources. When I have time, I need to track this down.

For the time being make sure you import the TMP Essential Resources before hand.

I’m having a similar issue in 2019.3 version doing:

public class LoginFillEmail : MonoBehaviour
{
TMP_InputField inputFieldEmail;
void Start()
{
inputFieldEmail = GetComponent<TMP_InputField>();
inputFieldEmail.textComponent.SetText(“a@b.com”);
inputFieldEmail.textComponent.SetAllDirty();
inputFieldEmail.placeholder.SetAllDirty();
}
}

After doing this I obtain the attached pic.
Also the event “On End Edit” is not triggered.
If I Click in the field, the current text is cleared.

Hope this helps, Thanks :wink:

5540584--570082--Captura.PNG

the next tow lines of code works only together…
inputField.text = “some text”;
inputField.textComponent.SetText(“some text”);