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. Dismiss Notice

Accessing an array with a string

Discussion in 'Scripting' started by elephantmobiledev, Mar 11, 2021.

  1. elephantmobiledev

    elephantmobiledev

    Joined:
    Jan 30, 2021
    Posts:
    7
    Hi. This is my firs post of the forum and my English is not perfect. So, if i do something wrong, sorry already now.

    I have arrays in my project and i want to access one of them with a string. For example;

    Code (CSharp):
    1. //I have this arrays.
    2. public string[] ARGENTINAcities;
    3. public string[] BRAZILcities;
    4. public string[] PERUcities;
    5. public string[] MEXICOcities;
    6.  
    7. //I have a Text.
    8. public Text cityname;
    9.  
    10. public void countryclick()
    11.     {
    12.         //When i click the button, i get its name as a string with "countryname".
    13.         //Like this, i click the "ARGENTINA" button and "countryname" becomes "ARGENTINA".
    14.         string countryname = EventSystem.current.currentSelectedGameObject.name;
    15.         //arrayname is that what i want to use to access arrays.
    16.         //Like this, arrayname = ARGENTINAcities;
    17.         arrayname = countryname + "cities";
    18.         //And here, i want to use "arrayname" as an array name to access its elements.
    19.         //Like this, cityname.text = arrayname[2];
    20.     }
    21.  
    22.  
    I will be glad if you help me.
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,769
    You would need to put each array into a Dictionary to find it.

    Code (csharp):
    1. public Dictionary<string,string[]> CityLists = new Dictionary<string,string[]>();
    And then in Start():

    Code (csharp):
    1. CityLists["ARGENTINA"] = ARGENTINAcities;
    2. CityLists["BRAZIL"] = BRAZILcities;
    Then you can access them with:

    Code (csharp):
    1. string CityName = "ARGENTINA";
    2.  
    3. string[] CityList = CityLists[CityName];
    I highly recommend you look into using JSON and loading that instead. That way if you add cities or countries, you do not need to recompile your program code.

    Another way of organizing it is to place each array of city names into a ScriptableObject, which also gets you away from changing code every time you change data.
     
  3. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    You could also use a switch statement.

    Code (csharp):
    1. public string[] GetCitiesArray(string countryName)
    2. {
    3.     string[] returnArray = null;
    4.  
    5.     switch(countryName)
    6.     {
    7.         case "ARGENTINA":
    8.             returnArray = ARGENTINAcities;
    9.             break;
    10.         case "BRAZIL":
    11.             returnArray = BRAZILcities;
    12.             break;
    13.         case "PERU":
    14.             returnArray = PERUcities;
    15.             break;
    16.         case "MEXICO":
    17.             returnArray = MEXICOcities;
    18.             break;
    19.         default:
    20.             Debug.Log("Uhhh, ohhh, GetCitiesArray called with a bad countryName: " + countryName);
    21.             break;
    22.     }
    23.     return returnArray;
    24. }
     
    Putcho and elephantmobiledev like this.
  4. elephantmobiledev

    elephantmobiledev

    Joined:
    Jan 30, 2021
    Posts:
    7
    Thank you so much Kurt. This is a very useful solution and I will learn JSON using. Thank you again.
     
    Kurt-Dekker likes this.
  5. elephantmobiledev

    elephantmobiledev

    Joined:
    Jan 30, 2021
    Posts:
    7
    Thanks for your time and answer me Joe. I will keep switch/case statement in my mind and i will try to use it often. Thank you again.
     
    Joe-Censored and Kurt-Dekker like this.