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
0
Question by donimation · Dec 12, 2016 at 11:38 PM · textstringnoobinputfieldlength

How to detect the length of an input field [C# noob]

Hello,

I'd like a button to pop up when an input field has at least 1 character in it. I'm struggling with the syntax; everything I try gives me a:

"NullReferenceException: Object reference not set to an instance of an object"

I tried using .Length but suspect that that only works for strings. So I tried to use .ToString to convert to something that can use .Length but am not having any luck with that approach either.

What steps/conversions do I need to follow/implement? An example with C# syntax would be ideal.

Thanks in advance,

Best regards, Don

Comment
Add comment · Show 5
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 AlucardJay · Dec 13, 2016 at 12:13 AM 0
Share

Posting your code will help identify the cause of the null reference, and understand your approach. eg, the .text variable of an InputField is already a string.

One method would be to utilize the OnValueChange callback event from the InputField component. So for every change to the field, check the length of the .text value

https://docs.unity3d.com/$$anonymous$$anual/script-InputField.html

avatar image donimation AlucardJay · Dec 13, 2016 at 03:11 AM 0
Share

@alucardj Thank you for the feedback! The reason I didn't post any code was because towards the end I was trying all sorts of different things and was deleting them as I went along. In retrospect, I should have at least posted my last attempt; maybe then my request for "example syntax" would have been more justified and as a result I might have been able to avoid that down-vote.

Lesson learned.

Evidently, as I mention bellow, the reason I was getting that error was because I tried to access a reference from another script. I thought I could do that and I'm sure there's probably a way, just haven't figured it out yet.

Anyway, problem solved; thanks again!

avatar image getyour411 donimation · Dec 13, 2016 at 03:19 AM 0
Share

As this just came up in another question, I had the link handy (for script to script references)

http://answers.unity3d.com/questions/42843/referencing-non-static-variables-from-another-scri.html

Show more comments
avatar image Pengocat · Dec 13, 2016 at 03:26 AM 0
Share

$$anonymous$$aybe you could do this in a script placed on the InputField GameObject! It deactivate whatever Button you assigned to it when nothing is entered.

 using UnityEngine;
 using UnityEngine.UI;
 
 public class InputFieldTest : $$anonymous$$onoBehaviour
 {
     public Button button;
     private InputField inputField;
 
     void Start()
     {
         inputField = GetComponent<InputField>();
     }
 
     void Update()
     {
         if (inputField.text == string.Empty)
         {
             if (button.gameObject.activeInHierarchy)
             {
                 button.gameObject.SetActive(false);
             }
         }
         else
         {
             if (!button.gameObject.activeInHierarchy)
             {
                 button.gameObject.SetActive(true);
             }
         }
 
     }
 }

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by getyour411 · Dec 13, 2016 at 01:38 AM

// Add UI namespace

 using UnityEngine.UI;

Setup references to myInputField and myButton. In the Editor, drag&drop these UI elements from hierarchy into script fields that will show up; alternatively use GameObject.Find, private [SerializeField] field, etc...

 public InputField myInputField;
 public Button myButton;

In Update (or similar)

 if (myInputField.text != "") {
 myButton.SectActive(true);
 }
Comment
Add comment · Show 2 · 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 donimation · Dec 13, 2016 at 02:49 AM 0
Share

@getyour411 thank you for the elegant if-statement solution in your comment, it workded! I wish you'd have posted it as an answer ins$$anonymous$$d of a comment so I can mark it "Best Answer"; it wouldn't let me do it.

Anyway, I think the reason I was getting the "NullReferenceException: Object reference not set to an instance of an object" error was because I was trying to access a reference from another script (that always gets me.) Once I created the reference in the script that was calling the if statement it worked like a charm!

Thanks again!

avatar image getyour411 donimation · Dec 13, 2016 at 02:53 AM 0
Share

Glad it worked; I modified my comment to be an Answer and your Answer to be a comment. If you are happy, tick the Accept mark

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

65 People are following this question.

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

Related Questions

Check if 2 InputField contains 2 words (not the same)? Feel like it's easy but can't figure out what's wrong 0 Answers

Create new text file everytime the game runs 1 Answer

Why is this List only showing one int? 0 Answers

Line breaks "\n" not formatting when string is modified, is there a work around? 1 Answer

Prevent escape key from deleting InputField contents, have it save instead? 4 Answers


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