Count and find NON-PERSISTENT UnityEvent listeners: How?
Is there any way to find out how many and which listeners have suscribed to a UnityEvent at runtime? There's methods like GetPersistentEventCount etc., but I am looking for NON-PERSISTENT listeners.
Context: I'm checking if anyone's subscribed to an event before firing it and it always returns true, even though it shouldn't:
public static UnityEvent UnshieldReds = new UnityEvent();
//somewhere else stuff subscribes, unsubscribes when adequate
public void UnshieldRedEnemies() {
if ( redsShielded ) {
//this always returns true, even though persistent event count is 0
if ( UnshieldReds != null ) {
UnshieldReds.Invoke();
shieldsDownSound.Play();
}
redsShielded = false;
}
}
In the given example, UnshielReds is invoked even though there are no red enemies with shields around that could have subscribed to the event, which is puzzling me. There must be a way when using the observer pattern to find observers, right?
Answer by jason_pocketpinata · Apr 12, 2018 at 10:21 PM
There doesn't appear to be, sadly, it makes no sense, and is yet another example of a Unity-ism.
This guy has a workaround: https://stackoverflow.com/questions/47836748/why-i-cannot-reach-non-persistent-listeners-count-from-my-unityevent