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 /
  • Help Room /
avatar image
0
Question by OtsegoDoom · Jun 08, 2017 at 01:25 AM · listsclassesnested

Adding to List of Nested Classes

Hi,

I've got an issue with nested classes that I've been struggling with all day. Below is a simplified example of what I'm trying to accomplish. Basically I want to read a list of all the characters in the game and then each character has a list of equipment objects attached to them. I'm using lists because the number of characters or equip-able items will be constantly changing. Everything shows up as expected in the inspector and the script compiles properly. When I run the app though, I get a null reference exception on the following line:

 characterList[a].equipmentList.Add(new Equipment() );

It seems I cannot access "equipmentList" to add a new item. I've been digging through Unity Answers and Google and haven't found anything quite like my issue. Is it possible this is a limitation of Unity? If I can get this working I will actually end up with a 2nd level nested class, but as it is, I can't get the 1st level working so I may have to rethink my entire structure.

Any feedback would be greatly appreciated.

Thanks!

 using System.Collections.Generic;
 
 [System.Serializable]
 public class Character {
     public string characterName;
     public List<Equipment> equipmentList;
 }
 public List<Character> characterList;
 
 [System.Serializable]
 public class Equipment {
     public string itemName;
     public int itemWeight;
 }
 
 void Start () {
 
     for (int a = 0; a < 10; a++) {
         characterList.Add(new Character() );
 
             for (int b = 0; b < characterList.Count; b++) {
                 characterList[a].equipmentList.Add(new Equipment() );
             }
         }
     }
 }

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
0
Best Answer

Answer by jdean300 · Jun 08, 2017 at 06:12 AM

You never initialize the equipmentList.

 for (int b = 0; b < characterList.Count; b++) {
     if (characterList[a].equipmentList == null)
         characterList[a].equipmentList = new List<Equipment>();
     characterList[a].equipmentList.Add(new Equipment() );
 }

In some cases, Unity will initialize a serializable list for you if it is null, but you cannot rely on it.

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 OtsegoDoom · Jun 08, 2017 at 01:08 PM 0
Share

That totally works, thank you!

I never thought to initialize the equipment list since I didn't have to do that for the character list.

I was pulling my hair out over this one. You just made my day!

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

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

Related Questions

Using the "Contain" method on a list with various variables. 0 Answers

Class with a List of Classes containing a List of Classes 1 Answer

How do I implement lists for this? Or should I? 0 Answers

Collection was modified; enumeration operation may not compute 0 Answers

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


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