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

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);
    }
}

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*.name == nameYoureLookingFor) {*

return Persons*.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.