Home > Net >  Unity c# check for an empty string always return false
Unity c# check for an empty string always return false

Time:09-30

I have the following code to check if in text component attached to my gamobject contains an empty string,

if (string.IsNullOrEmpty(inputFieldText.text) || string.IsNullOrWhiteSpace(inputFieldText.text) || inputFieldText.text == "" || inputFieldText.text.Trim() == string.Empty )
        {
            //Do my stuff
        }

as a matter of fact, using the debugger (see the image attached), I can see that the string is empty but for some reason all of the check methods that I've tried return false. What am I missing?

image

CodePudding user response:

Just a wild guess into the dark but you seem to be using the TextMeshProUGUI component of the input field (assuming this from the name inputFieldText).

So I assume this to be related to this https://forum.unity.com/threads/float-parse-does-not-work-in-tmpro-input-field-which-basically-means-tmpro-is-useless.718268/#post-4804799

Perhaps related...

manager.networkAddress = IPField.GetComponent<TMPro.TextMeshProUGUI>().text.Trim();

...includes a Unicode Zero Width Space (U 200B) on the end that Trim() doesn't remove.

That is correct in terms of why the float.Parse would fail. However, and more importantly, this is due to referencing the child text component instead of the parent TMP_InputField and its .text property.

=> change the type to

public TMP_Inputfield inputFieldText;

assign it again via the Inspector and try again with simply

...
}
else if(string.IsNullOrWhiteSpace(inputFieldText.text))
{
...
  • Related