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 Fadi · Jun 29, 2015 at 03:04 AM · arraylistgeneric listgeneric.list

how add values to Generic.list ?

Hi...

how can I add these values to the generic.list array

Name = Jone

Age = 29

Photo = image for him

Family

  • Name = Nadia

  • Age = 49

  • Photo = image for her

  • Name = Saied

  • Age = 58

  • Photo = image for him

one person and two family members

     public List<PersonsData> Persons;
     [System.Serializable]
     public struct PersonsData
     {
         public string Name;
         public int Age;
         public Texture2D Photo;
         public List<FamilyData> Family;
     }
     
     [System.Serializable]
     public struct FamilyData
     {
         public string Name;
         public int Age;
         public Texture2D Photo;
     }





thank you so much

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
5
Best Answer Wiki

Answer by starikcetin · Jun 29, 2015 at 03:33 AM

First of all, you need to declare lists like this:

 public List<PersonsData> Persons = new List<PersonsData>();

Then you can add values like this:

 PersonsData pd = new PersonsData();
 
 Persons.Add(pd);

Good luck!


Edit: I personally not prefer giving direct code answers but, here take a look at this:

 [System.Serializable]
 public struct PersonsData
 {
     public string Name;
     public int Age;
     public Texture2D Photo;
     public List<FamilyData> Family;
 }
 
 [System.Serializable]
 public struct FamilyData
 {
     public string Name;
     public int Age;
     public Texture2D Photo;
 }
 
 public class example
 {
     public List<PersonsData> Persons = new List<PersonsData>();
 
     public void _example ()
     {
         PersonsData jone = new PersonsData();
 
         jone.Name = "Jone";
         jone.Age = 29;
 
         jone.Family = new List<FamilyData>();
 
         FamilyData nadia = new FamilyData();
 
         nadia.Name = "Nadia";
         nadia.Age = 49;
 
         jone.Family.Add(nadia);
 
         FamilyData saied = new FamilyData();
 
         saied.Name = "Saied";
         saied.Age = 58;
 
         jone.Family.Add(saied);

         Persons.add(jone);
     }
 }




Comment
Add comment · Show 5 · 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 Fadi · Jun 29, 2015 at 01:52 PM 0
Share

Thank you for answering me ...

I write this code :

         PseronsData pd = new PseronsData();
         FamilyData fd  = new FamilyData();
 
         pd.Name = "Jone";
         fd.Name = "Saied";
         pd.Family.Add (fd);
         Persons.Add(pd);


but I got this error message on pd.Family.Add (fd);

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

how can i add family members ?

avatar image Piflik · Jun 29, 2015 at 02:19 PM 0
Share

You have to first create the list itself, before you can add items. (Or create the List with the items)

 pd.Family = new List<FamilyData>();
 pd.Family.Add(fd);

or

 pd.Family = new List<FamilyData> { fd };
avatar image Fadi · Jun 29, 2015 at 05:34 PM 0
Share

thank you soooooooo much :)

avatar image starikcetin · Jun 29, 2015 at 05:38 PM 0
Share

I edited the answer with a not-tested code. Take a look at it.

avatar image Fadi · Jun 30, 2015 at 10:09 AM 0
Share

thank you , it's just an example to know how to add values to list , but this not my project .... it's just an example

now I can add it to my project , :)

thank you again ....

avatar image
0

Answer by malkere · Jun 29, 2015 at 03:32 AM

you can just do Persons.Add(new PersonsData) to create a new entry in the list and then Persons[0].Name = whatever, etc. but Lists change their index automatically when stuff is input and removed. You might want to try a Dictionary instead so you can store the Value (PersonsData) with a Key (Name) Then you would call the PersonsData with Persons[nameYoureLookingFor].

alternatively if you don't have a lot of entries you can just

 for (int i = 0; i < Persons.count; i++) {
    if (Persons[i].name == nameYoureLookingFor) {
       return Persons[i].Age
    }
 }

that will loop through all the entries of the list looking for what you're trying to find.

Adding is easy, but make sure you know if you should be using a List, an Array, or a Dictionary before you start. They all have their ups and downs.

Comment
Add comment · Show 3 · 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 starikcetin · Jun 29, 2015 at 03:41 AM 0
Share

Using a foreach statement for code you wrote would make things easier:

 foreach(var item in Persons)
 {
     if(item.Name == nameWeLookFor) return item.Age;
 }
avatar image Fadi · Jun 29, 2015 at 01:53 PM 0
Share

thank you for answering me ...

I need to add person and family data to the list array

avatar image malkere · Jun 29, 2015 at 02:31 PM 0
Share

Piflik has the answer above. A null reference means you are trying to access something that doesn't exist yet. All lists and arrays and dictionaries and what not need to be initialized before they can be used. examples of initilization:

 //define the list:
 List<string> strings;
 //then initialize it:
 strings = new List<string>(); //<-- need the ()
 //or do it all at once:
 List<string> strings2 = new List<string>();
 
 //define the dictionary:
 Dictionary<string, int> ints;
 //then initialize it:
 ints = new Dictionary<string, int>(); //<-- need the ()
 //or do it all at once:
 Dictionary<string, int> ints2 = new Dictionary<string, int>();
 
 //define the array:
 string[] stringArray;
 //then initialize it:
 stringArray = new string[10]; //<-- no () needed, but must define its dimensions, in this case 10 strings, 0-9
 //or do it all at once:
 string[] stringArray = new string[10];

can then add freely to any of them now that they are initizlized:

 strings.Add("newStrings");
 ints.Add("newString", 42);
 stringArray[0] = "newString";

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

23 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

Related Questions

How can I check if ALL items in an array/list meet a condition? 2 Answers

A node in a childnode? 1 Answer

Emptying a Generic List / Unexpected Behaviour 1 Answer

Generic List.Count always gives 0 2 Answers

Count selected item in List 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