Set property when an event triggers?

Say I have a class that has a property, a float called health. It has a setter.

    public float Health
    {
        set
        {
            health = value;
        }
    }

Either in this class or I guess anywhere else, there is an event that sends out a float:

public event Action<float> EventWithFloat;

Can I somehow hook up calling the setter of the property to be called when that event is triggered? Without writing an extra method (that calls the property).

So something like EventClass.EventWithFloat += Health;

Thanks

There may be a way on higher levels of C# but I’m not aware of those.

You can always just wrap it with a lambda:

EventWithFloat += (x) => { Health = x; };
1 Like

Awesome, that looks great. I saw a line similar to that in the official oculus quest scripts, there it looked like they stripped the variable from an event to subscribe a function to it that doesn’t have any parameters.

Once again thanks for help!

1 Like