Check if InputField is empty
How do you check if the input field is empty? I got an error when deleting the whole text in the InputField. The input field content is set to decimal numbers only.
Answer by Glurth · Mar 07, 2016 at 05:08 PM
You simply need to examine the text value of the input field to see if it is blank...
if(inputField.text=="")
Debug.Log("field is empty");
http://docs.unity3d.com/ScriptReference/UI.InputField-text.html
Years late update: Regarding checking a string in general.. Hellium has a better/more complete/bulletproof way to check them in the comments below:
string.IsNullOrEmpty( inputField.text )
hey guys,how to use it on gui lable with if statement;thank you ,how to show with gui label
Add a
public Text WarningText;
at the top of your classDrag & drop the text gameObject in the inspector
In your code :
if( string.IsNullOrEmpty( inputField.text ))
{
WarningText.text = "Input is empty";
WarningText.gameObject.SetActive( true );
}
else
WarningText.gameObject.SetActive( false );
It doesn't work. It says "Input string was not in correct format", maybe that has to do with it not working??
You are most likely trying to use a function like int.Parse
, which is not related with the problem of Anoimus.
That's not the main problem. (I think) When the you type something in the input field, it does what it should. But when you remove what you typed, the input field doesn't go back to its original state or at least set itself to 0. I've tried the (inputField.text == "") and also (string.IsNullorEmpty(inputFieldname.text) and it also didn't work. I did write so that if that is true the inputField.text = 0 and stuff.