Dude, can of worms. Definitely possible but if you’re not advanced enough, chances are you’re opening up a can of worms. Then again, I have nothing to loose so I’ll share.
What you’re looking for is called language reflection, or introspection, which is a programming language’s ability to look at its own properties (and modify them). The full write-up of how to use reflection for C# can be found here, but I’ll cut to the crux.
Here’s an example of a MonoBehaviour using C# reflection to modify its name property without directly referencing its name property/variable…
using UnityEngine;
using System.Collections;
public class NameToBlorp : MonoBehaviour {
// Find the closest point on AB to a point
void Awake()
{
// Examining the name of all variables in a C# object
// In this case, we'll list the variable in this NameToBlorp
// class
System.Reflection.PropertyInfo [] rProps = this.GetType().GetProperties();
foreach(System.Reflection.PropertyInfo rp in rProps )
Debug.Log( rp.Name );
// Getting the info of a specific variable name.
// This gives us the ability to read/write it
System.Reflection.PropertyInfo propName = this.GetType().GetProperty( "name" );
if( propName != null )
{
// The PropertyInfo isn't the actual variable, just the "idea" of
// the variable existing in an object.
//
// It needs to be used in conjunction with the object...
// Equivalent of this.name = "blorp"
propName.SetValue(
this, // So we specify who owns the object
"blorp", // A C# object as the value, will be casted (if possible)
null
);
// And GetValue can be used in a similar fassion.
// Equivalent of Debug.log( "..." + this.name )
Debug.Log( "The name is " + propName.GetValue( this, null ) );
}
}
}
Obviously this is going to run slower because there’s more stuff going on and should be avoided unless absolutely necessary. Just have the script know the type of the object and access it directly or through a class function instead (i.e PlayerStats.variable = bleh).
You could make a method on your PlayerStats class to convert a string into a float.
public float GetProgress(string attribute){
switch (attribute){
case "Strength":
return strength;
case "Wisdom":
return wisdom;
case default:
return 0;
}
}