Search Unity

How to call an event from a child class

Discussion in 'Scripting' started by Giustitia, May 15, 2020.

  1. Giustitia

    Giustitia

    Joined:
    Oct 26, 2015
    Posts:
    113
    Hello everybody!

    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:

    Code (CSharp):
    1. public class ParentClass : MonoBehaviour
    2. {
    3.     public delegate void MyDelegate();
    4.     public event MyDelegate myEvent;
    5. }
    6.  
    7. public class ChildClass : ParentClass
    8. {
    9.     private void Start()
    10.     {
    11.         myEvent.Invoke(); //This is not valid
    12.     }
    13. }
    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 :p
     
  2. csofranz

    csofranz

    Joined:
    Apr 29, 2017
    Posts:
    1,556
    Perhaps re-visit some OOP and Unity tutorials.

    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.
     
    Giustitia and nailmail like this.
  3. Giustitia

    Giustitia

    Joined:
    Oct 26, 2015
    Posts:
    113
    As far as I know, delegates and events doesn't need to be initialized (opposite to UnityEvent). That's the error I get

    If I try to do the same from the parent class, all works perfectly
    Code (CSharp):
    1. public class ParentClass : MonoBehaviour
    2. {
    3.     public delegate void MyDelegate();
    4.     public event MyDelegate myEvent;
    5.  
    6.     private void Start()
    7.     {
    8.         myEvent.Invoke(); //This compiles
    9.     }
    10. }
    11.  
    12. public class ChildClass : ParentClass
    13. {
    14.     private void Start()
    15.     {
    16.         myEvent.Invoke(); //This DOESN'T compiles
    17.     }
    18. }
    In this case I don't need UnityEvents at all
     
  4. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    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.
     
    nailmail and Giustitia like this.
  5. Giustitia

    Giustitia

    Joined:
    Oct 26, 2015
    Posts:
    113
    Thanks @Suddoha , it seems that I can keep working and not re-visiting "some OOP tutorials" by now :p