Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Storing Information In Array

Discussion in 'Scripting' started by solideuk, Oct 12, 2015.

  1. solideuk

    solideuk

    Joined:
    Nov 29, 2014
    Posts:
    41
    Hi

    I am trying to set up an array to store information relating to a particular item.

    If I wish to store information on a particular item that contains both a string and integer, how would I go about doing this?

    For example if I wish to create a table for fruits with quantity sold, would I use a multidimensional array and select string as type and convert the string to integer when required?

    Many thanks
     
  2. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    sounds more like a dictionary, but you've not explained the use case in much detail...
     
  3. martinmr

    martinmr

    Joined:
    Mar 25, 2015
    Posts:
    325
    For an array that stores a name and a value

    Code (CSharp):
    1. public class ItemInformation {
    2.     public string name;
    3.     public int value;
    4. }
    5.  
    6. public class YourOtherClass : MonoBehaviour {
    7.  
    8.     private ItemInformation[] items = new ItemInformation[5];
    9.  
    10.     private void DoSomethingMethod() {
    11.  
    12.         items[X].name = "string";
    13.         items[X].value = 42;
    14.  
    15.         //or
    16.  
    17.         for (int i = 0; i < items.Length; i++) {
    18.             items[i].name = "string";
    19.             items[i].value = 42;
    20.         }
    21.     }
    22. }
    But like @LeftyRighty said, depending on your use case also a Dictonary would work or if you are adding items to something also a List<ItemInformation> would be ok.
     
  4. solideuk

    solideuk

    Joined:
    Nov 29, 2014
    Posts:
    41
    Thanks.

    Example of scenario:

    Item Type: Quantity:
    Apple 10
    Orange 15

    The item types will be predefined, the quantity will be changed accordingly.
     
  5. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    apple etc. only ever exists once? I'd definitely go with a dictionary


    if you're after something more like

    monday apple 10
    tuesday apple 5

    etc.

    you might want to go with an array/list of structs that hold the 3 bits of information.
     
  6. solideuk

    solideuk

    Joined:
    Nov 29, 2014
    Posts:
    41
    Thanks will give this a go.

    How efficient is this method (for tablet)?

    As the item name will be predefined searching for a particular record should be easy as I can simply access using items[0] etc
     
  7. solideuk

    solideuk

    Joined:
    Nov 29, 2014
    Posts:
    41

    That's correct apple etc will only appear once.
    It's likely that the column numbers would increase, it may be beneficial in that case to go with array.

    The example above, that is an array and not an array list, is that correct?

    Edit:

    For the scenario described, would a playerpref method be better than using an array method?
     
    Last edited: Oct 12, 2015