Search Unity

Question How do i create a LIST OF ARRAYS and add something to it?

Discussion in 'Scripting' started by BL4CK0UT1906, Nov 27, 2022.

  1. BL4CK0UT1906

    BL4CK0UT1906

    Joined:
    Apr 15, 2022
    Posts:
    26
    Sorry for my english.
    I have found almost no material on that on the web. I must have been searching for the wrong key terms.

    But i simply want to create an list of arrays so i can store my inventory itens.
    I want to store something like "item name" and "quantity" ("[potion,3]").

    How can i do that?
    Thanks.
     
  2. jlorenzi

    jlorenzi

    Joined:
    May 2, 2021
    Posts:
    292
    I think custom classes or scriptable objects would be better for this than creating a list of arrays.
    But if you want to create a list of arrays this would work
    Code (CSharp):
    1. public List<string[]> abc;
     
    BL4CK0UT1906 likes this.
  3. BL4CK0UT1906

    BL4CK0UT1906

    Joined:
    Apr 15, 2022
    Posts:
    26
    I have tried using a custom class but i just can't do it. And like i said, i haven't been able to find much tutorials on the subject.
    Thanks for the answer.
    How do i add a value to it? i can't find the right way...
     
  4. tleylan

    tleylan

    Joined:
    Jun 17, 2020
    Posts:
    618
  5. Trindenberg

    Trindenberg

    Joined:
    Dec 3, 2017
    Posts:
    398
    Code (CSharp):
    1.         Dictionary<string, int> Inventory = new();
    2.  
    3.         string TestItem1 = "Chicken";
    4.  
    5.         if (Inventory.ContainsKey(TestItem1))
    6.             Inventory[TestItem1]++;   // Add 1 to quantity/int
    7.         else
    8.             Inventory.Add(TestItem1, 1);  // Add new item, quantity 1
     
  6. rickitz5

    rickitz5

    Joined:
    Aug 8, 2021
    Posts:
    31
    In all honesty as @Trindenberg points out, a c# Dictionary is a much better and faster choice for what you intend to achieve.

    Not only does this allow two distict variable types, but also is super quick to lookup data.

    However if you really did want to go down the route or arrays in a list, you can do it like this -
    Code (CSharp):
    1. List<string[]> itemListArr = new List<string[]>();
    2. itemListArr.Add(new string[2] { "Potion", "3" });