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

Question How to access an array by its name?

Discussion in 'Scripting' started by Biltekin, Apr 13, 2023.

  1. Biltekin

    Biltekin

    Joined:
    Jan 18, 2016
    Posts:
    14
    I just want to access an array by its name but I don't know how to do without using Dictionary<>.

    For example :
    Code (CSharp):
    1. public int[,] array_no1 = new int[8,128];
    2. public int[,] array_no2 = new int[8,128];
    3. //...
    4. public int[,] array_no100= new int[8,128];
    I want to access one of them and assign it to a new array :

    Code (CSharp):
    1. string arrayNameStr = "array_no" + Random.Range(1, 101);
    2. int[,] tempint = [arrayNameStr];

    Any help would be appreciated, thanks.
     
  2. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    6,015
    Well the 'obvious' method would be to use reflection, though that should be avoided unless absolutely necessary.

    Ideally you should have some kind of collection of these collections. You could easily just have a List<T> that all these are stored in (rather than writing out 100 fields like a madman), and can generate a random integer index to access them.
     
  3. Olipool

    Olipool

    Joined:
    Feb 8, 2015
    Posts:
    316
    You could also have a mapping method that will take a string as input and give the according array by using a huuuge switch expression but it is a bit clunky and similar to the Dictionary which you don't want.
     
    Biltekin likes this.
  4. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    4,382
    That seems totally pointless. Why not just put all of those arrays in an array and access the Random.Range(1, 101) by index? I mean, the whole point of the array is so you don't have to all this crazy variable1, variable2, variable3 stuff.
     
    orionsyndrome and Chubzdoomer like this.
  5. orionsyndrome

    orionsyndrome

    Joined:
    May 4, 2014
    Posts:
    3,043
    Because OP clearly doesn't appreciate the whole point of arrays or indice. OP doesn't seem to notice the pattern.

    edit:
    I still fondly remember having to come up with an array-like behavior in the original Actionscript for Flash 5 (1999) where the solution was to fiddle with the name -- like OP wants to do here -- because it was all name-based associative arrays under the hood. But this was an early form of this light scripting language and arrays were nowhere in sight yet.
     
  6. Biltekin

    Biltekin

    Joined:
    Jan 18, 2016
    Posts:
    14
    Thanks all of you for the replies. I asked because I was wondering if there is a method like this or not, thought it would be more readable code in my case, gonna use array of array
     
  7. orionsyndrome

    orionsyndrome

    Joined:
    May 4, 2014
    Posts:
    3,043
    Do not ever assume that fiddling with strings is recommended in any form unless you're specifically working with strings as data. This is true in every production language nowadays. Strings are really cumbersome to work with, always located in a far memory, and in C# they're also immutable. Always, always, always, avoid leaning onto strings for your day-to-day control code. Strings are used strictly as input or as output, for example serialization, static text, and GUI.

    I shared that anecdote in my previous post to show that I understand where you're coming from, but with so much tools at our disposal in modern C#, you have to eradicate all such thoughts of temporarily converting anything to string just to move along, that's just bad, period.
     
    Biltekin likes this.