vendredi 24 novembre 2023

How can I add value to a reflected variable?

I want to add value to a value I got through reflection. The += does not work and SetValue does not fit my needs. *

  • Instance.Field does exist but this would not be good for my script(For reasons).

Here is my class:

public class OreMine : MonoBehaviour
{
    [SerializeField] string VariableName;

    void OnCollisionEnter2D(Collision2D col)
    {
        if(col.gameObject.tag == "Player")
        {
            var Value = typeof(Inventory).GetField(VariableName, BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static);
            Destroy(gameObject);
        }
    }
}

The problem is that I could not do this:

Value += 1

And this would not suit my needs:

public class OreMine : MonoBehaviour
{
valueField.SetValue(null, SomeInteger);
}

And in Microsoft documentation the AddValue seems like it's only for strings.

https://learn.microsoft.com/en-us/dotnet/api/system.runtime.serialization.serializationinfo.addvalue?view=net-8.0





Aucun commentaire:

Enregistrer un commentaire