- Home /
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
$$anonymous$$eans you don't have a text component on it. What kind of object is it? UI text? Empty?
The first thing is to find out exactly what it is that's null. Find out by breaking the line down. In other words, stuff like...
Text t = gameObject.GetComponent<Text>();
if (t == null) { // report an error }
if (inputField == null) { // report an error }
textField t2 = inputField.GetComponent<textField>();
if (t2 == null) { // report an error }
and so on.
I have the same problem. Storing a reference to a UI Text or Button in a class member variable (start function) and then using it later in any other method of the same class always raises a null exception even if it was not null when it was requested first. I have no explanation for this.
Answer by Superrodan · Feb 03, 2015 at 11:52 PM
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.
Without the right using
statements, the code wouldn't compile, giving you compile errors rather than runtime exceptions. You need using UnityEngine.UI;
anytime you reference a class that comes from that namespace.
The Text
class is one such class. No matter where you use it: in a private field, method signature, or generic method (like GetComponent{Text}()
), you'll always need that using
to get your program to build.
I'd agree with @awplays49's comment on the OP.
Thanks for your reply, but I have already included the UnityEngine.UI; statement and the problem still persists. Thanks though for your help :D
Answer by VOTRUBEC · Feb 04, 2015 at 10:01 AM
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.
Answer by Mmmpies · Feb 04, 2015 at 09:55 AM
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.
Your answer
![](https://koobas.hobune.stream/wayback/20220613182405im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Why is this null? Finding a script on an object 3 Answers
Object reference not set to an instance of an object in c#. 0 Answers
NullReferenceException. GetComponent dose not work properly 2 Answers
NullReferenceException - Editor script using GetComponent after AddComponent 1 Answer
NullRef on my parent script 1 Answer