Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Events in interfaces or abstract classe

Discussion in 'Scripting' started by yulaw2k, Aug 24, 2016.

  1. yulaw2k

    yulaw2k

    Joined:
    Apr 25, 2014
    Posts:
    31
    Normally I would use an interface to allow me to have any object with any script on which I can call the method activate(). Because I don't know if it is a door, car, monster or banana....

    In this case I would like to have that same functionality but with Events. Instead of calling the interface method. I would like to subscribe to the Event of the object without caring what type of script/object it has.

    I spent sometime searching to no avail.

    I tried to create an abstract class which has the delegate and event.

    Please do not correct Syntax...just typing this off the top of my head, I am sure it has compiler errors, this is just a quick sketch of what I am doing. But please do correct what I am doing wrong.

    public abstract class ActivatorEvents{
    delegate....blah blah....
    event... ActivateEvent
    //Because only the class that created the event can invoke
    Protected void CallEventActivated(){
    ActivateEvent();
    }
    }

    public class Valve : ActivatorEvents {

    blah blah blah
    }

    In another class when I do.

    ActivatorEvents.ActivateEvent += SomeMethod();

    Nothing ever happens, but if I delete the abstract class, and throw the delegate and event into the Valve class, it works fine.
     
    Last edited: Aug 24, 2016
  2. yulaw2k

    yulaw2k

    Joined:
    Apr 25, 2014
    Posts:
    31
    Is there a delete thread button?
    I solved my problem. My Activate Event was calling the wrong event........ So the example code above works.