Build variable name using string

For my game I want to load a variable based on a string. The string is made up of standard text plus the ID of the specific object (so I only need one script that can handle actions for lots of objects). The script looks like this (and doesn’t function):

string reference = "list" + id;
List<int> thisLevels = OtherStaticScript.reference;

It’s pretty obvious to me that it won’t work like this, but still it was worth a try.

So is there a way to make this work or a different approach to this issue?
Thank you in advance for your help!

I had a similar issue, what you need is a Dictionary. It is basically a list that has a ‘key’. The key can be an int, string, char you name it. With the key you can search for fields in the Dictionary.

I believe you can do something like

string reference = "list" + id;
list<int> thisLevels = OtherStaticScript.GetType().GetProperty(reference).GetValue(OtherStaticScript,null);

I actually don’t think this works on arrays or lists but it should be adapted I’m on my phone so hard to write it out complete… GL