Assign a value to a variable choosen by a string.

Hi, sorry my english. It´s posible to find a variable with a string?
Example:

var variablename_ : String;

variablename_ ++;

What I´m doing is a game when you take something, then sum up the universal variable that count how much of this I have. Example:

var whatis_ : String;

if (whatis_ = “RedPlant”)
variables.redplant_ ++;

if (whatis_ = “BluePlant”)
variables.blueplant_ ++;

if (whatis_ = “RedRock”)
variables.redrock_ ++;

and etc…

Please!!!!

Hi what would you mean by options?

2 Answers

2

This has been asked quite a lot.

I’d read this answer

Make a Dictionary<string, int> itemCounts, enter initial values

itemCounts.Add("blueThing", 0);
itemCounts.Add("redThing", 0);

and increment with itemCounts[_whatis]++

Thanks, but what I want is replace the second example for the first.
To avoid having to use an IF for each one. I put the name of the universal var in the variablename_ string and then, using this string, add a value.

Anyone, please.