Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by sillanstudios · Feb 03, 2015 at 11:42 PM · nullreferenceexceptiongetcomponent

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

Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image awplays49 · Feb 03, 2015 at 11:48 PM 0
Share

$$anonymous$$eans you don't have a text component on it. What kind of object is it? UI text? Empty?

avatar image Bonfire-Boy · Feb 04, 2015 at 09:14 AM 1
Share

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.

avatar image movAX13h · Apr 21, 2015 at 02:13 PM 0
Share

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.

3 Replies

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Alanisaac · Feb 04, 2015 at 01:15 AM 2
Share

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.

avatar image sillanstudios · Feb 04, 2015 at 07:18 AM 0
Share

Thanks for your reply, but I have already included the UnityEngine.UI; statement and the problem still persists. Thanks though for your help :D

avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

24 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges