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
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
@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!
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
$$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);
}
}
}
}
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);
}
@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!
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