• 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
1
Question by Lairinus · Sep 28, 2012 at 06:35 AM · c#nullreferenceexception

C# NullReferenceException with GetComponent issue

I'm having another problem when it comes to GetComponent, as it says it's coming up as a null reference.

I've attempted making the code many different ways but this always pops up.The current test code that I'm applying is:

 public class anotherTest : MonoBehaviour 
 {
     public GameObject Miles;
     // Use this for initialization
     void Start () 
     {
         Miles.GetComponent<test>().Health = 400;
         Debug.Log (Miles.GetComponent<test>().Health);
         
         
     }
     
     // Update is called once per frame
     void Update () 
     {
         if (Input.GetKeyDown(KeyCode.B))
         {
             Miles.GetComponent<test>().Health = Miles.GetComponent<test>().Health + 400;
             Debug.Log (Miles.GetComponent<test>().Health);
             
         }
     
     }
 }

After fighting with GetComponent for awhile, I've turned to this as being one of the only ways I've found that compiles without fail.

The base class that the code is coming from is literally a monobehavior class with just a public int "Health."

The only thing I'm trying to accomplish with this is to see the values every time I hit B to make sure it works. Every time I hit B, though, I get hit with the error. (and also at initialization)

I'm probably missing at least 1 line of code, so could anyone try to point me in the right direction?

Thanks in advanced for helping sort out this issue!

Comment
Add comment · Show 1
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 Dreamblur · Sep 28, 2012 at 06:38 AM 0
Share

If that script is derived from the [test] class and you're reading it as a [test] class, then it won't know what the [$$anonymous$$iles] variable is.

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by aldonaletto · Sep 28, 2012 at 06:40 AM

Well, if this compiles ok, the problem probably is the script test.cs not being attached to the object referenced by Miles, or Miles was not assigned in the Inspector.

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 Lairinus · Sep 28, 2012 at 06:57 AM 0
Share

I just assigned both scripts to the same object and now it's compiling-- didn't know that was a necessity lol

Thanks for the help. $$anonymous$$inda odd that it's done that way though.

Originally I had 1 sphere with the test class and 1 cube with the anotherTest class. I originally thought that as long as they derived from $$anonymous$$onoBehaviour, you could pull them from anywhere if they're attached to a gameObject.. together or not

avatar image aldonaletto · Sep 29, 2012 at 02:27 PM 0
Share

GetComponent searches for components attached to a single object only. If you write just GetComponent, this is compiled as this.GetComponent, and the object is the one that holds the script where GetComponent is called. If you write SomeObject.GetComponent, the object is the one referenced by SomeObject. It must be this way, since many objects may have the same component - you must specify in which object to search.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

11 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Can't load Mouse Cursor texture 1 Answer

RaycastHit2D not interacting with tags 1 Answer

NullReferenceException and m_InstanceID == 0 on LoadLevel (C#) 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