- Home /
Unity Events notifications
Hello! I am implementing events in unity like that: I have components : "Damage" and "Shoot" and "LocalEventsManager" in LocalEventsManager I have :
public delegate void delTakeDamage;
public delTakeDamage event eTakeDamage;
public void OnDamageTaken(){
if(eTakeDamage!=null){
eTakeDamage();
}
}
In each component of this object I get the "localEventsManager" component and thanks that I have communication beetwen components - for example if Character takes damage I trigger getComponent<LocalEventsManager>().OnDamageTaken();
and in "Shot" somponent I listen to that and when damage taken I can do function like "CancelShoot". (In this way I listen to that: " getComponent<LocalEventsManager>.eTakeDamage+= CancelShoot;
If I want to notify some objects about global events like "OnPlayerDeath" I use Global Events Manager which is a Singleton or static class.
Please tell me- is it a good enough way to handle it or is there an easier way? For example maybe there is a posiblity to efficiently create one Global Events Manager which also handle events for single gameobjects? (for example Goblin3 took damage and only his components need to know that. Not every single gameobject in scene.)