Can't use GetComponent in C#

Hello guys!

I can’t seem to get GetComponent to work in C#. It keeps throwing me a nullreference exception. It’s getting really annoying and is crashing my game. Here’s my code:

void Update () {
	//strLad = inputField.GetComponent<textBox>().textField.text;
	gameObject.GetComponent<Text>().text = inputField.GetComponent<textBox>().textField.text;
}

I understand how it all works. The components that are scripted to be accessed on the specific gameObjects are actually there. This script is working within a prefab and it’s children.
If you could help me out I’d really appreciate it. Can’t work it out, very confusing.
Thanks

This is just a guess, but do you have

using UnityEngine.UI;

At the top of your script? I know when I use Text elements I need that there, but I’m not sure you would need it for a GetComponent.

Are you using Unity on Windows, with Visual Studio? Visual Studio Community Edition is free.

If so, get the Unity VS ‘plugin’. This way you’ll be able to attach the solution to Unity, set a break point on “Line 5”, then run the project. At the point the breakpoint is reached, you’ll be able to see EXACTLY which item is null.

This works for me:

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

public class GetInputFrom : MonoBehaviour {

	public InputField myInputField;
	public Text myText;

	public void FilledIn()
	{
		myText.text = myInputField.text;
	}
}

The in the EndEdit click + and drag the script in, I just put the script on the target Text UI component so just drag the into the inspector slot and from the dropdown select GetInputFrom → FilledIn.

For ease I just made them public objects for testing but you can reference them how ever you normally would.