Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 naqvir · Mar 28, 2012 at 06:46 PM · gameobjectnullreferenceexceptiongetcomponentnull

Why is this null? Finding a script on an object

Can someone please tell me why i keep getting a nullReferenceExceptionError. I am trying to access a script attatched to the controller gameobject.

 public var controller:GameObject;

 var getEssenceHandlerComponant:Component = controller.GetComponent("GetEssenceHandler");

 function Update() {
 
 }

 function LateUpdate () {
 var number:float = getEssenceHandlerComponant.lifeEssenceCarrier;
 number = Mathf.Floor(number);
 gameObject.Find("Value").guiText.text = number.ToString() + "%";
 }

It is saying there is a null exception at line 2 of the code. Ive checked my getEssenceHandler script is attached to the controller object.

Comment
Add comment
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

3 Replies

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

Answer by Eric5h5 · Mar 28, 2012 at 07:01 PM

You should almost never use strings in GetComponent, nor should you use Component as the type. Also, stick to declaring variables only outside functions, and run code inside functions. Also, don't use Find every frame in Update, do it once in Start and cache the result.

 private var getEssenceHandlerComponent : GetEssenceHandler;
 private var valueGUIText : GUIText;
 
 function Start () {
     getEssenceHandlerComponent = controller.GetComponent(GetEssenceHandler);
     valueGUIText = GameObject.Find("Value").GUIText;
 }

If getEssenceHandlerComponent is null, that means the controller object doesn't have that component. Also, in JS, public is default for variables, if you want private you have to specify it. You don't need to write ToString if you're concatenating a number with a string, and you're using some unnecessary steps.

 function LateUpdate () {
     valueGUIText.text = Mathf.Floor (getEssenceHandlerComponent.lifeEssenceCarrier) + "%";
 }
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
1

Answer by naqvir · Mar 28, 2012 at 06:55 PM

It seems you cannot reference the a variable if both are not in a method. You have to do this:

 public var controller:GameObject;
 private var getEssenceHandlerComponant:Component;
 
 function Awake() {
 getEssenceHandlerComponant = controller.GetComponent("GetEssenceHandler");
 }
 
 
 function Update() {
     //if(getEssenceHandlerComponant.) // gui 
  }
 
 
 function LateUpdate () {
     var number:float = getEssenceHandlerComponant.lifeEssenceCarrier;
     number = Mathf.Floor(number);
     gameObject.Find("Value").guiText.text = number.ToString() + "%";
 }
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
1

Answer by rutter · Mar 28, 2012 at 06:58 PM

Did you set controller in the inspector?

Are you sure that the component's name is spelled exactly as you have it listed?

Double check that the component is attached to the object you're trying to access.

Finally, you might try moving that GetComponent() call into an Awake() function, like so:

 var otherGuy : OtherGuyComponent;

 function Awake()
 {
     otherGuy = GetComponent(OtherGuyComponent);
 }

I personally don't recommend using GetComponent with string arguments. It works, but doesn't expose programming errors as early and only gives you generic "component" reference, which usually isn't ideal.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Assign collider and linerenderer to list 1 Answer

UnityEngine.GameObject.GetComponent[WeaponBehavior] 1 Answer

script GetComponent, nullReference error 1 Answer

NULL REFERENCE EXCEPTION,NULL REFERENCE EXCEPTION while accessing child objects unity2D 0 Answers

How do I nullreference this correctly? 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