Accessing Current Text Within an Input Field

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.

It seems like there is no Text component on “InputText”.

So try this way:

public InputField input;
public Text output;
public void doTheTransfer()
{
       output.text=input.text;
}

While sleeping on it, I came up with a solution that works without any overhead (like unnecessary event handling or public variables). My original script was attached to the canvas and took no parameters. This new script is attached to the output box and takes the input box as a parameter. It works flawlessly!

using UnityEngine;
using UnityEngine.UI;
using System.Collections;

public class BetterButtonTransferScript : MonoBehaviour
{
    public void doTheTransfer(GameObject inputField)
    {
        InputField input = inputField.GetComponent<InputField>();
        Text output = transform.GetComponent<Text>();
        output.text = input.text;
    }
}