I'm trying to do a kind of debug console in Unity to be able to change - for example - enable/disable boolean values at run-time.
There's a point where I have a value that I want to set in a certain variable but this value is stored as a string (input from the user) and I need to cast it to the type of that variable (stored in a Type variable) but I don't know if this is possible.
This is the part of my code where I have the problem:
private void SetValueInVariable(string variable, Type type, string toSet)
{
//reflection - https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/reflection
Type container = typeof(StaticDataContainer);
FieldInfo placeToSet = container.GetField(variable, BindingFlags.Static);
placeToSet.SetValue(null, //here I need to convert "toSet");
}
I would like to know if this is possible and how can I do it.
varvalue, for that matter :-)