You indeed want to use event so that any entity can broadcast info to any others:
public class BroadcastingEntity : MonoBehaviour
{
public static event Action<BroadcastingEntity> RaiseLowHealth,
protected void OnLowHealth()
{
if(RaiseLowHealth != null) { RaiseLowHealth(this); }
}
public static event Action<BroadcastingEntity> RaiseDeath,
protected void OnDeath()
{
if(RaiseDeath!= null) { RaiseDeath(this); }
}
}
then you have the actual entity:
public class Entity : BroadcastingEntity
{
private int health = 50;
public int Health
{
get{ return this.health; }
set
{
this.health += value;
if(this.health <= 0)
{
OnDeath(this);
return;
}
if(this.health < 10) { OnLowHealth(this); }
}
}
}
finally, you'd have a centralized system to listen to that, let's say your player.
public class Player : MonoBehaviour
{
void Start()
{
BroadcastingEntity.RaiseLowHealth += EntityLowHealth;
BroadcastingEntity.RaiseDeath += EntityDeath;
}
void OnDestroy()
{
BroadcastingEntity.RaiseLowHealth -= EntityLowHealth;
BroadcastingEntity.RaiseDeath -= EntityDeath;
}
private void EntityLowHealth(BroadcastingEntity entity){ }
private void EntityDeath(BroadcastingEntity entity){ }
}
The event is static but the parameter is a reference to the entity that triggers the event. So you can print name or get some other component on it.
You can do :
private void EntityLowHealth(BroadcastingEntity entity)
{
print(entity.GetType());
}
this will give the actual subtype of the entity, so you can perform all kind of actions