While you can technically do anything in a property than you can in a method, so therefore could call events and input.
It’s counter to the implied behaviour of a property.
Properties are methods that “look like” fields from a syntax context. As a result properties are things that you should be able to read/write frequently (for example a while loop that reads a prop will read it many many times).
Basically a method implies “work is done” where as a property implies “state is read/set”.
(firing an event is “work”)
This is why you discovered that its causing stack over flows because of the frequency of access. I’m assuming your event handler is then reading the property causing the event to go off again causing it to read it causing it to go off again causing you to read it… ad infinitum.
A good rule of thumb is a property should mainly just validate incoming values, or resolve outgoing values.
Example:
private float _positiveValue;
public float PositiveValue
{
get { return _positiveValue; }
set { _positiveValue = Mathf.Max(0f, value); }
}
This validates that the incoming value is >= 0.
public float Debit;
public float Credit;
public float NetTotal
{
get { return Debit - Credit; }
}
Here NetTotal is resolving an outgoing value that isn’t actually directly stored, but rather is the difference of 2 other stored values.
…
This isn’t to say you can never have an event in a property.
A good example of one is say a “property changed” event:
public event EventHandler NamePropertyChanged;
private string _name;
public string Name
{
get { return _name; }
set
{
if(_name == value) return;
_name = value;
NamePropertyChanged?.Invoke(this, EventArgs.Empty);
}
}
Note though that it’s done in the “set” event, and the event implies it would raise during the set. Which means if you set the Name property during the event handler it’s obvious you’re reraising the same event.
Where as in your original example… you’re doing it in the “get” event (which is weird… why would “getting” a value raise an event? I assume you were trying to do this so as to avoid having to read the value AND raise the event in place. In which case… create a “float ReadAndRaiseLeftTriggerInputValue()” method). There’s no reason for me to think looking at the name that the property would be doing this for any reasonable reason…
It’d be like if I set up my house to make coffee any time I looked at the clock. Why would I do that? Why would such a passive action do that? I don’t necessarily want coffee EVERY time I read the clock… it only just so happens I read the clock at 6am I am also making coffee. That’s incidental, they aren’t necessarily tied to one another.