Is it possible to use enum value as a variable name,Is it possible to use enum value as a variable name?

Lets say I have an enum

public enum Colors {
      Black,
      White,
      Red
    }; 

public Colors Brush;

Then I have ScriptableObject with colors variables defined:

public class ColorsLib: ScriptableObject
{
    public Color Black = new Color32(0,0,0,1);
    public Color White= new Color32(255,255,255,1);
    public Color Red = new Color32(255,0,0,1);
}

The question is: am I able to use enum values as a names of scriptable obj without creating dictionary, like this:

public Color Palette;

Palette = ColorsLib.Brush[1]; //I know this is incorrect syntaxes but the idea is I want to grab ScriptableObj variable by name taken from enum... But Brush[1] is White (isnt just a string right?) and I want grab this White from my ColorsLib somehow

So I can create an additional dictionary when I ll write something like “Black” = ColorsLib.Black; and then use enum Brush[1].ToString() to grab the value from dictionary but I would like to avoid this

Yes this should be possible. I am not familiar with it but can point you in the right direction for this.

Basically everything that concerns itself that the code has to know something about how it was written (as in for example “what was the variables name”) we talk about “reflection”. This is where in c# the library Linq comes in.

Check out this thread on Stack overflow:
Thread Link

There it is discussed how this can be done. There should be a getProperty function which can be called with a string as argument. Now the string can be the name of your enumeration value by which you can then make this connection without the need for a dictionary.

Be advised though that some Linq operations can be quite heavy on performance. So perhaps consider if a dictionary is the correct solution but you can use Linq to automatically build this dictionary so you don’t have to update it manually.