#Invoking Action with no subscribers
1 messages · Page 1 of 1 (latest)
A delegate/event that has no subscribers should be null. You can safely invoke it by doing OnHit?.Invoke()
The null ref exception you got should have made that clear
Thanks
Related note, I recommend use of the event keyword:
public event Action OnHit;
I thought it was somehow trying to invoke the first element of an empty list. Didn't think it would be null itself
Yes, that's what I'm doing
Great 👍
Thanks for the help
btw, you may have heard that ?. isn't safe to use in Unity
this is specifically for unity objects -- anything inheriting from UnityEngine.Object
it's "unsafe" because it only checks if the object is actually a null reference
unity objects have overloaded the == operator so that obj == null is true when obj is a destroyed unity object
?. will not check for this, so obj?.transform will throw an error if obj is destroyed
But that is not relevant in the case of actions, right?