I’m trying to access the value a user has placed in an Input Field, but it’s not going well. For simplicity’s sake, I’ve whipped up a thing trying to do nothing but update a static text field with the value in an editable one once a button is clicked. This is what I’ve got:
using System.Collections;
using UnityEngine;
using UnityEngine.UI;
public class ButtonTransferScript : MonoBehaviour
{
public void doTheTransfer()
{
Text input = transform.Find("InputText").GetComponent<Text>();
Text output = transform.Find("OutputText").GetComponent<Text>();
output.text = input.text;
}
}
When I run my app and click the button that calls this function, I wind up with a null pointer exception error because the input variable is null on the last line. Every solution I’ve been able to find has either not worked for me or invoked event handling on the Input Field (which is unnecessary overhead).
What’s the Component type I’m missing that would allow me to access the current text in an Input Field?
Worth noting: output.text = “foobar”; on the last line works as you’d expect it to - it’s very confusing that an identical structure on the input text field would return a null reference.