I'm currently creating a C# script for Unity containing many variables, and I want these to be changeable from other scripts.
To do so, I've currently written a function which asks the name of the variable (as a string) and then uses a switchcase for each variable.
public string charName;
public int currentHP;
public int maxHP = 100;
public int strength;
public int defense;
public void ChangeValue(string valueToChange, int howMuch)
{
switch (valueToChange)
{
case "currentHP":
currentHP += howMuch;
break;
case "maxHP":
maxHP += howMuch;
break;
case "strength":
strength += howMuch;
break;
case "defense":
defense += howMuch;
break;
}
}
Would there be a more efficient way of doing this ? The switch case seems really inefficient, I'd need to modify it everytime I want to add a new variable to the script.
The perfect thing for me would be to ask the variable as an argument, but I can't figure out a way to do it.
Thank you for your time !
interfaceand expose it?enuminstead of strings