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 Sendatsu_Yoshimitsu · Sep 13, 2014 at 08:07 PM · c#programmingclassconstructors

Constructor giving nullreference error

I'm trying to use a basic class to hold data on each NPC in the scene, and then assign values to said class during character initialization. The data class reads:

 public class CharacterAffinity {
     public int characterID;
     public GameObject characterObject;
     public int characterAffinity;
 
 
     public CharacterAffinity(int charID, GameObject charObject, int charAffinity){
         characterID = charID;
         characterObject = charObject;
         characterAffinity = charAffinity;
     }
 
 }

and I call it while iterating through a list of NPCs:

     for (int i = 0; i < characterList.Length; i++) {                //Build list of all characters and initialize values
         if (characterList[i] != gameObject){    //Keep from adding self to list
             CharacterAffinity tempAffinity;
             tempAffinity = new CharacterAffinity(characterList[i].GetComponent<CharacterSheet>().CharacterID,characterList[i],UnityEngine.Random.Range(0, 50));
             affinityList.Add(tempAffinity);
         }
     }


The code compiles, but it gives me an error for each call to the constructor, "Object reference not set to an instance of an object". Is there an obvious error in my constructor/class syntax that I'm missing?

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 VesuvianPrime · Sep 13, 2014 at 08:29 PM 1
Share

I'm assu$$anonymous$$g characterList[i].GetComponent() is returning null.

You're doing an awful lot on that one line, try breaking it into multiple, smaller lines for easier readability and debugging.

avatar image Fanttum · Sep 13, 2014 at 08:33 PM 0
Share

One of your public variables might not be set in the inspector. If not set there it would need to be set in start() or awake(), unless you call your CharacterAffinity(…) within one of those. Not quite sure, but that's my guess.

avatar image Sendatsu_Yoshimitsu · Sep 13, 2014 at 08:38 PM 0
Share

I checked each of the individual values in the tempAffinity line and they're all working properly, and debug.logs show that characterList[i].getcomponent is returning a value. If I add [Serializable] above my class declaration the errors go away, but I don't know if that's the fix or a band-aid hiding a bigger problem.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Paulius-Liekis · Sep 15, 2014 at 08:55 AM

Why do you think it's the constructor? I would guess it's more likely to be on this line:

 characterList[i].GetComponent<CharacterSheet>().CharacterID
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 Sendatsu_Yoshimitsu · Sep 16, 2014 at 08:32 PM 0
Share

I'm reasonably half-certain the problem isn't on that line, simply because swapping out dummy/filler values to guarantee that it isn't getting a null value doesn't eli$$anonymous$$ate the error.

avatar image Sendatsu_Yoshimitsu · Sep 16, 2014 at 09:22 PM 0
Share

So I did some further testing, if the class is serializeable it works fine, if I remove [serializable] from the class declaration it can't find CharacterID, and returns 0. When I mark as serializable, however, it finds and returns the correct value.

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

25 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 avatar image

Related Questions

Multiple Cars not working 1 Answer

How to make costume classes Visible in the Inspector 3 Answers

Problem with getting a value from a enum 2 Answers

I'm already an experienced programmer, is there anywhere great to go to get familiar with the libraries available in unity? 2 Answers

Instantiating Prefabs through Editor 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