Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. Dismiss Notice

Question using a string to automate adding items to arrays

Discussion in 'Scripting' started by Trussmister, May 24, 2024.

  1. Trussmister

    Trussmister

    Joined:
    Mar 24, 2024
    Posts:
    24
    HI, i will be very shocked if this is possible but i have a lot of variables all with "Defaults" at the end and i want to store all of them into an array using strings to change the variable referenced in a for loop each cycle, thanks in advance.
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,947
    What you're describing is how you might do something in a dynamically typed language such as Python or JavaScript, but not in a strongly typed language like C#.

    To do something similar you need to learn how to use Dictionaries in C#. Python and JavaScript objects are basically just Dictionaries in disguise.

    Here's a small tutorial on dicitonaries: https://www.tutorialsteacher.com/csharp/csharp-dictionary
     
    Bunny83 likes this.
  3. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    21,684
    You can but be aware that the value types (ie ints, floats, structs, etc) are boxed when converted to
    object
    which comes with a performance penalty and allocates memory. Additionally the approach below requires you to either come up with a way to track what type is being stored or remember what you've stored it as when you read it.

    https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/types/boxing-and-unboxing

    Code (csharp):
    1. var dictionary = new Dictionary<string, object>();
    2.  
    3. dictionary.Add("x", 5);
    4. dictionary.Add("y", 3.14);
    5. dictionary.Add("z", new List<int>());
    For example (written by GPT-4o).
    Code (csharp):
    1. var dictionary = new Dictionary<string, (Type, object)>();
    2.  
    3. dictionary.Add("x", (typeof(int), 5));
    4. dictionary.Add("y", (typeof(double), 3.14));
    5. dictionary.Add("z", (typeof(List<int>), new List<int>()));
    6.  
    7. // Checking the type
    8. void CheckTypeAndPrint(string key)
    9. {
    10.     if (dictionary.TryGetValue(key, out var valueTuple))
    11.     {
    12.         Type type = valueTuple.Item1;
    13.         object value = valueTuple.Item2;
    14.  
    15.         if (type == typeof(int))
    16.         {
    17.             int intValue = (int)value;
    18.             Console.WriteLine($"Key: {key}, Type: int, Value: {intValue}");
    19.         }
    20.         else if (type == typeof(double))
    21.         {
    22.             double doubleValue = (double)value;
    23.             Console.WriteLine($"Key: {key}, Type: double, Value: {doubleValue}");
    24.         }
    25.         else if (type == typeof(List<int>))
    26.         {
    27.             var listValue = (List<int>)value;
    28.             Console.WriteLine($"Key: {key}, Type: List<int>, Value: {listValue}");
    29.         }
    30.         else
    31.         {
    32.             Console.WriteLine($"Key: {key}, Type: {type}, Value: {value}");
    33.         }
    34.     }
    35.     else
    36.     {
    37.         Console.WriteLine($"Key: {key} not found in the dictionary.");
    38.     }
    39. }
    40.  
    41. // Example usage
    42. CheckTypeAndPrint("x");
    43. CheckTypeAndPrint("y");
    44. CheckTypeAndPrint("z");
     
    Last edited: May 24, 2024
    Bunny83 likes this.
  4. tleylan

    tleylan

    Joined:
    Jun 17, 2020
    Posts:
    692
    I read the replies but it seems to me that this is not what you are asking. Maybe an example (examples are generally a good idea). From the sounds of it my answer will be "don't do that" :)
     
  5. Trussmister

    Trussmister

    Joined:
    Mar 24, 2024
    Posts:
    24
    Thanks so much!
     
  6. Trussmister

    Trussmister

    Joined:
    Mar 24, 2024
    Posts:
    24
    Thanks but i need to use it in other classes
     
  7. Trussmister

    Trussmister

    Joined:
    Mar 24, 2024
    Posts:
    24
    oh i just knew how