• 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 JerkyTreatz · May 04, 2015 at 11:21 PM · component

NullReference exception on gameObject.AddComponent<>()

I am trying to add a "NPCNeed" component to an NPC gameobject.

Because I cannot use constructors, I have an Init method on my base Need class

 abstract Need : Monobehaviour

 NPCNeed : Need

I have an NPCNeedFactory class responsible for creating and attaching all the different Needs to the NPC.gameObject that calls it.

However when I state:

 //Attaches a new Empty NPCNeed to character (passed through in method sig)
 NPCNeed charNeed = character.gameObject.AddComponent<NPCNeed>();

 //This does not throw an exception
 Debug.Log(charNeed);

 //Init fills out the variables for NPCNeed
 charNeed.Init(  //NullReference throw here: Object reference not set to an instance of an object
     j["NPCNeed"][i]["needName"].Value, 
     NeedStatusFactory(j),
     j["NPCNeed"][i]["startingValue"].AsInt,
     j["NPCNeed"][i]["valueDecrementRate"].AsInt, 
     j["NPCNeed"][i]["timeToDecrement"].AsInt //Code doesn't get here, these just pull vars from  a JSON script
 );

I can post more code if needed, but I want to believe that I'm missing something obvious. Why hasn't charNeed become the object reference Init() is looking for?

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

2 Replies

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

Answer by DiegoSLTS · May 05, 2015 at 02:35 AM

Have you debugged your code? I mean with a breakpoint.

I guess that charNeed isn't null, the whole call to the "Init" method is the problem and it is 1 "line", try getting all those values outside the Init call on separate variables. One of them should throw the NullPointerException before reaching the charNeed.Init line.

Note that any of this steps could return a null pointer:

 j["NPCNeed"]
 j["NPCNeed"][i]
 j["NPCNeed"][i]["needName"]
 j["NPCNeed"][i]["startingValue"]
 j["NPCNeed"][i]["valueDecrementRate"]
 j["NPCNeed"][i]["timeToDecrement"]

Even "j" can be null there.

Comment
Add comment · Show 1 · 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 JerkyTreatz · May 05, 2015 at 01:26 PM 0
Share

Ah, I didn't realize that a failure in the call would result in a failure at the method line number. Easy enough to pull out. Thanks.

avatar image
0

Answer by Polak149 · May 05, 2015 at 02:09 PM

You are getting nullrefrence cause charNeed is null. Debug will also write "null".

Try this: NPCNeed charNeed = character.gameObject.AddComponent(NPCNeed);

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.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

2D Animation does not start 1 Answer

How to Get TextMesh component from the Gameobject the script is attached to? 3 Answers

SendMessage or store reference and call directly? 1 Answer

How to add a script to an imported object 2 Answers

A basic question about collision 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