Search Unity

more than one list

Discussion in 'Scripting' started by megawhile, Oct 20, 2019.

  1. megawhile

    megawhile

    Joined:
    Oct 20, 2019
    Posts:
    21
    hey there, iam new in unity and dont know how use more then one list

    Code (CSharp):
    1. public class Mylist : MonoBehaviour
    2. {
    3.     [SerializeField] public List<Mylist_Data> datlist= new List<Mylist_Data>();
    4.  
    5.     void Start()
    6.     {
    7.         gen_zrlist();
    8.     }
    9.  
    10.     void gen_zrlist() {
    11.         datlist.Add(new Mylist_Data(0, "test"));
    12.  
    13.     }
    14. }
    i will make datlist[0], datlist[1], datlist[2] but dont know how i should read that...
    can anybody help me pls?

    and the 2. problem is how can i have arrays in list struct like this:
    Code (CSharp):
    1. [System.Serializable]
    2. public struct Mylist_Data
    3. {
    4.  
    5.  
    6.     public int ID;
    7.     public string name;
    8. //public int[] numbers=new int[12];
    9.  
    10.  
    11.  
    12.  
    13.     public Mylist_Data(int ID, string name, int[] numbers)
    14.     {
    15.      
    16.         this.ID = ID;
    17.         this.name = name;
    18. this.numbers=numbers;
    19.    
    20.     }
    21. }
    datlist.Add(new Mylist_Data(0, "test", array_numbers));
    is not working, how can i read it correct?
     
  2. Olmi

    Olmi

    Joined:
    Nov 29, 2012
    Posts:
    1,553
    Hi and welcome,

    Just create multiple lists? Based on what you told this sounds more like a C# question than Unity related issue.

    Code (CSharp):
    1. public List<Stuff> stuffs = new List<Stuff>();
    2. public List<Thing> things = new List<Thing>();
    3.  
    You don't initialize Lists like you suggested (...will make datlist[0], datlist[1], datlist[2]...)

    Your code contains syntax errors too. If you want to have an array inside the struct, you need to set it up correctly:
    Code (CSharp):
    1. public struct Mylist_Data
    2.     {
    3.         public int ID;
    4.         public string name;
    5.         public int[] numbers;
    6.  
    7.         public Mylist_Data(int ID, string name, int[] numbers)
    8.         {
    9.             this.ID = ID;
    10.             this.name = name;
    11.             this.numbers = numbers;
    12.         }
    13.     }
    14.  
    You need to have that [] there in the declaraction. Otherwise it's just an int type. Don't set a size as you will insert a reference to an array that you have created elsewhere.

    When you tried: "datlist.Add(new Mylist_Data(0, "test", array_numbers));"
    You probably wrote something wrong/had syntax errors, but try this (for example):
    Code (CSharp):
    1. datlist.Add(new Mylist_Data(1, "name", new int[] { 0, 1, 2, 3} ));
    You could also initialize your array like this:
    Code (CSharp):
    1. int[] putIntsHere = new int[3];
    2. putIntsHere[0] = 3;
    3. putIntsHere[1] = 4;
    4. putIntsHere[2] = 5;
    And then create that instance of your Mylist_Data struct:
    Code (CSharp):
    1. datlist.Add(new Mylist_Data(1, "name", putIntsHere));
    It might be worth checking C# reference manual page about collection initializers (lots of good examples etc.):
    https://docs.microsoft.com/en-us/do...nd-structs/object-and-collection-initializers
     
    Last edited: Oct 20, 2019
  3. megawhile

    megawhile

    Joined:
    Oct 20, 2019
    Posts:
    21
    create new array in the struct script was probably a mistake by me, thanks for the enlightenment :)

    but back to the first problem, datlist1 = new List<> datlist2, datlist3 works fine, but only manually, how can I generate multiple lists automatically? 10-20 lists depending on ... there would be something like datlist [0], datlist [1] very good, have u any idea?
    my bad idea is to create 10-20 empty game objects and add the class script with datlist, it works, but the other way would be smoother i think ...

    thxs 4 link it was nice
     
  4. Olmi

    Olmi

    Joined:
    Nov 29, 2012
    Posts:
    1,553
    How about:
    Code (CSharp):
    1.  public List<List<int>> listOfLists = new List<List<int>>();
    Now you have a List that can contain arbitrary number of Lists that can contain type of int.

    Then you can populate that list as needed, for example you could add 2 or 100 lists in a for loop. Or whatever you need to do. Or remove lists.

    Here's an example how you can add a new list (that has one data element) to your list of lists:
    Code (CSharp):
    1. var aNewList = new List<int>();
    2. // Add stuff to the new list
    3. aNewList.Add(42);
    4. // Add this new list to the "master" list
    5. listOfLists.Add(aNewList);
    Hopefully this helps you.
     
  5. megawhile

    megawhile

    Joined:
    Oct 20, 2019
    Posts:
    21
    idk i try it, but i think make gameobject with class is now the easier way, because the list in the list cant more than one type(int/ string ...) or? i try but the problem was every that idk how make more values than one , the second problems with list in list is System.Serializable for public showing in inspector ist not working without more programming code and 4 this my brain is to empty now... lol i programming weeks to get public multiple arrays in inspector and then i see lists are the easier way, now i see gameobjects with classes are the easier way, sometimes dirty code is better or have u any other idea 4 me?
    i have just tried to nested a list into a list to make it easier to open up in the inspector since you can open and close, but game objects+class with parents hierarchy can open and close, too, this will probably be the easier way i think...
     
  6. Olmi

    Olmi

    Joined:
    Nov 29, 2012
    Posts:
    1,553
    Honestly I have no idea what you are trying to accomplish so it's hard to give you any good suggestions. What are you trying to do?
     
  7. megawhile

    megawhile

    Joined:
    Oct 20, 2019
    Posts:
    21
    sorry my english is so bad... here a pic:

    i try to get every values to see in the inspector to open and close easy,
    in "Name" field i want a new list with arrow(open and close) and more and more lists, almost infinite :D


    that works almost infinite, its the faster way to code, but maybe not the best way 4 performance...idk
     
    Last edited: Oct 22, 2019
  8. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    You want to nest lists within each other? This should work:
    Code (csharp):
    1. [System.Serializable]
    2. public class MyListData {
    3. public int id;
    4. public string name;
    5. public List<MyData> children;
    6. }
     
  9. megawhile

    megawhile

    Joined:
    Oct 20, 2019
    Posts:
    21
    did u really class mean? or struct? in class its no problem, but in struct not working
    Code (CSharp):
    1.  
    2. [LIST=1]
    3. [*]public struct Mylist_Data
    4. [*]    {
    5. [*]        public int ID;
    6. [*]        public string name;
    7. [*]        public int[] numbers;
    8. [*]public List<Mylist_Data> children;
    9.  
    10. [*]
    11.  
    12. [*]        public Mylist_Data(int ID, string name, int[] numbers, List<Mylist_Data> children)
    13. [*]        {
    14. [*]            this.ID = ID;
    15. [*]            this.name = name;
    16. [*]            this.numbers = numbers;
    17. [*]this.children=children;
    18.  
    19. [*]        }
    20. [*]    }
    21. [/LIST]
    22. }
    VisualStudio Code: CS7036
     
  10. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    This works just fine with structs as well as classes; there's some other issue with your code.

    Copy and paste the entire error, not just the error code; the rest of the message contains really important information. Based on what this error is it looks like the problem is probably where you call the constructor (which you didn't show).
     
  11. megawhile

    megawhile

    Joined:
    Oct 20, 2019
    Posts:
    21
    oh yeah right, u are the right man, it works really fine ! :D thx u both so much :)

    last problem was i give the new list the same struct as children, memory overflow, unity crashed

    now iam happy :D