public class MyClass: MonoBehaviour
{
// The scripts which triggers the event, add it threw the inspector of using GetComponent<ListenToMe >() in the Awake function.
public ListenToMe EventCaller.
// The delegate which will contains the link to the function 'OnEvent' we want to call when the event is triggered.
private TheDelegate _handler;
void Start()
{
// Create the delegate with the function to call.
_handler
= new TheDelegate
(OnEvent
); }
// Use this for initialization
void OnEnable()
{
// Subscribe to the remote event
EventCaller.EventToListen += _hander;
}
void OnDisable()
{
// When you subscribe to an event with the +
// always, alwAYS, ALWAYS unsubscribe to it with the -
// otherwise your game will be full of leaks and then you die. (Oh wait you may not die but your application will be a spaghetti machine.)
EventCaller.EventToListen -= _hander;
}
// The function called when the event is triggered.
void OnEvent(GameObject pGameObject)
{
Debug.Log("Event triggered");
}
}