Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
2
Question by Anoimus · Mar 07, 2016 at 04:43 PM · uiinputfield

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.

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Hellium · Mar 07, 2016 at 12:37 PM 0
Share

Provide the script you have already written please

1 Reply

· Add your reply
  • Sort: 
avatar image
9

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 )
Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image omidhedayati · Mar 27, 2019 at 05:18 AM 0
Share

hey guys,how to use it on gui lable with if statement;thank you ,how to show with gui label

avatar image Hellium omidhedayati · Mar 27, 2019 at 10:54 AM 1
Share
  1. Add a public Text WarningText; at the top of your class

  2. Drag & drop the text gameObject in the inspector

  3. In your code :


  if( string.IsNullOrEmpty( inputField.text ))
    {
         WarningText.text = "Input is empty";
         WarningText.gameObject.SetActive( true );
    }
    else
         WarningText.gameObject.SetActive( false );

 




avatar image Miaw666 · Jan 06, 2020 at 10:56 PM 0
Share

It doesn't work. It says "Input string was not in correct format", maybe that has to do with it not working??

avatar image Hellium Miaw666 · Jan 06, 2020 at 11:04 PM 0
Share

You are most likely trying to use a function like int.Parse, which is not related with the problem of Anoimus.

avatar image Miaw666 Hellium · Jan 06, 2020 at 11:22 PM 0
Share

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.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

8 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

how can I make InputField Work ? 1 Answer

The UI Button Hitbox changes after using a Input Field on Android 1 Answer

Focused style for inputfield 0 Answers

How to disable SelectAll() text of InputField OnFocus()? 1 Answer

Check if EventSystem is in InputField 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges