Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 2-Zons · Nov 10, 2015 at 08:08 PM · listclassnull reference exception

List .add problem

OK, this one has been infuriating me. I am using many lists with no problems. I am making a list of my own class but it won't let me add an item to it. I get a runtime null reference error and I can't figure out why.

Here is my relevant code:

 public class LevelDetails
 {
     public float TargetHue;
     public int TargetCount;
     public List<GameObject> InvetoryList;
 
     public LevelDetails()
     {
         TargetHue = 0;
         TargetCount = 0;
     }
 }
 
 public class MyGameFunctions : MonoBehaviour
 {
       public List<LevelDetails> levelDeatails;
 
       void Start();
       {
       levelDeatails.Add(new LevelDetails());
        }
 }


The runtime error occurs at the .add method and is:

NullReferenceException: Object reference not set to an instance of an object

I've tried removing the list inside the class thinking that it might be the cause but it didn't make a difference.

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

1 Reply

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

Answer by DoTA_KAMIKADzE · Nov 10, 2015 at 08:09 PM

Change your Start function to this:

 void Start () 
 {
     levelDeatails = new List<LevelDetails>();
     levelDeatails.Add(new LevelDetails());
 }

or do like that:

 public List<LevelDetails> levelDeatails = new List<LevelDetails>();
 void Start () 
 {
     levelDeatails.Add(new LevelDetails());
 }

also notice that you don't need ";" after Start function line.

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 Bunny83 · Nov 10, 2015 at 11:02 PM 1
Share

@2-zons:

You also might want to make your custom class serializable. In that case you don't need to create the list since the inspector will create the list for you. However that only works when you add your "$$anonymous$$yGameFunctions" clas inside the editor. When you add it dynamically at runtime using AddComponent you have to initialize your list yourself.

 [System.Serializable]
 public class LevelDetails
 {
     //....

That way you can fill your list in the inspector.

avatar image 2-Zons · Nov 10, 2015 at 11:10 PM 0
Share

Thank you, that was it. very quick response :)

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

A node in a childnode? 1 Answer

How to sum a property of a class in a List 2 Answers

Can't call derived class function from list of base class. Ideas? 1 Answer

Need my function to work with different lists of different values (classes) 1 Answer

Displaying the players through GUI 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