Okay so here's my code:
Stat stat = (Stat)FieldFinder.FindField(character, skill);
Debug.Log(skill);
Debug.Log(stat);
Debug.Log(score);
stat.SetValue(score);
and here is FindField
public static object FindField(object obj, string fieldName)
{
Debug.Log("In object: " + obj);
Debug.Log("Finding field: " + fieldName);
Type type = obj.GetType();
Debug.Log("In type: " + type);
FieldInfo field = type.GetField(fieldName);
Debug.Log("FieldInfo: " + field);
if (field != null)
{
Debug.Log("Field found: " + field.Name);
object value = field.GetValue(obj);
Debug.Log("Value: " + value);
return value;
}
else
{
Debug.LogWarning($"Field '{fieldName}' not found in object of type '{type.Name}'.");
return null;
}
}
Now what is strange about it is that this code seems to work differently, based on wether I have an object with Character in it in my inspector. If I don't it finds a field as it is supposed to (at least I think so), but then Debug.Log(stat) tells me it is null. If it is in my inspector, it works as it is supposed to. Is it a known glitch and do you know how to fix it?