Overview | Videos | Documentation | Demos | Integrations | API | Support

Doc Home > Component Communication > Events

The EventHandler classes adds a generic event system to the framework. This event system allows objects to register, unregister, and execute events on particular objects. Multiple objects can subscribe to the same event. The Third Person Controller uses events extensively to allow components to communicate without directly knowing about each other. The following definition is used to listen for an event:

EventHandler.RegisterEvent(object obj, string name, Action handler)

This function is overloaded to allow events with multiple parameters. Unregistering from an event is very similar as it is to register for an event:

EventHandler.UnregisterEvent(object obj, string name, Action handler) 

An event can then be executed with:

EventHandler.ExecuteEvent(object obj, string name)

As an example, the "OnRespawn" event is executed when the Respawner component spawns the object again. A component can listen for this event by first registering:

EventHandler.RegisterEvent(gameObject, "OnRespawn", RespawnFunction);

Where RespawnFunction is the function that should execute when the "OnRespawn" event is executed. This function might look like:

private void RespawnFunction()
{
   // Respawn logic
}
The GameObject can then unsubscribe itself from the event with the following:

EventHandler.UnregisterEvent(gameObject, "OnRespawn", RespawnFunction);

The OnRespawn event will be called by the Respawner class by calling ExecuteEvent:

EventHandler.ExecuteEvent(gameObject, "OnRespawn");

Every object which is subscribed to the "OnRespawn" event of that particular GameObject.


<- Component Communication
Shared Property and Method ->