Change a variable with ToString()

Is there a way of doing this, I have a global variable that is on a global script called GlobalVars …

globalVarsScript.carType1Petrol[carNumber] = petrol;

what I’d like to do is change the 1 in carType1Petrol to 2, 3, ect … by using myInt.ToString() … can this be done as it’s a variable ? And when I try it says " is not a member of ‘GlobalVars’ "

You could use reflection. For example if PropertyName is a public property on MyClass and you have an instance of this class you could:

MyClass myClassInstance = ...
double temp = (double)typeof(MyClass).GetProperty("PropertyName").GetValue(myClassInstance, null);

If it’s a public field:

MyClass myClassInstance = ...
double temp = (double)typeof(MyClass).GetField("FieldName").GetValue(myClassInstance);

Of course you should be aware that reflection doesn’t come free of cost. There could be a performance penalty compared to direct property/field access.

You could make an enum for your car types.

enum CarTypes