Just a short and simple question: how to call (.Invoke()) an event from a child class? I have acces to the event, but I’m not able to call his .Invoke() function. Here’s an example:
public class ParentClass : MonoBehaviour
{
public delegate void MyDelegate();
public event MyDelegate myEvent;
}
public class ChildClass : ParentClass
{
private void Start()
{
myEvent.Invoke(); //This is not valid
}
}
As a workaround, I’m just creating a proxy function in the parent class that calls the Invoke, so child class can call this function, but I don’t like it so much
myEvent.Invoke() will work - if it is initialized. Where do you initialize myEvent? If you want to invoke a superclass method, there is nothing special to it.
Also, I stringly recommend you use UnityEvents instead of Delegates and Events, as they integrate much better with Editor.
That’s the way you have to do it when you use delegates with the event-modifier.
It’s actually correct what he said.
Using the event modifier only allows the declaring type to invoke and re-assign the event (the backing delegate actually).
Others, even subclasses, can only subscribe and unsubscribe feom it, unless methods for doing the above are provided.
The recommandation about unity events is highly subjective. I’d do the exact opposite… they are handy, but can be a nightmare.