Hi guys, currently working on a 2D platformer like survival. There will be lots of Enemies etc. so I had this question like for 2 days, couldn’t find any answers by myself.
Which is best to apply Damage and manage Health of the Enemies. I have this two types of Methodes;
Deleting Unity. (joking)
Creating a one Methode called Health Manager, It will loop and get all the enabled enemies from the Scene, Has a List of Stat for all indv. Enemy , long story short, one method will loop and apply damage (with doing the math)
Every Enemy will have ApplyDamage method
and I will call them like SendMessage("ApplyDamage "); for those who is in collider etc.
My advice would be to not think about performance optimizations at all right now. It doesn’t matter unless you are shipping a game and you have profiled and identified specific bottlenecks which prevent your game from achieving some specific platform requirements.
However, you should think about which code solution is more maintainable or “just works better” for you as the developer. I would not use SendMessage because it is not type safe and cannot be debugged as easily as other methods. For example, the receiving message is not compile-checked in any way, so you could accidentally delete it, and if you haven’t selected ‘requiresReceiver’ you won’t even get an error. Overall, the system is quite old and not recommended anymore.
For starters, I would create a “Health” component which implements a “IDamageable” interface. Enemies carry a weapon or some sort of “DamageDealer”, which can check via collision (OnTriggerEnter) if another IDamageable component is within the trigger. If so, apply the damage. No need for a Health Manager in my opinion. I assume “lots of enemies” is less than 100. Dealing with hundreds of enemies might require a different approach, but I wouldn’t tackle this issue without having tried the first version successfully.
There’s a related Unity tutorial about interface here.
And here is some code:
public class Health : MonoBehaviour, IDamageable
public int maxHealthPoints;
private int healthPoints;
private void Start()
// Start at full health.
healthPoints = maxHealthPoints;
public void ChangeHealth(int amount)
healthPoints += amount;
if(healthPoints < 0)
public interface IDamageable
/// Damage is applied by a negative amount,
/// Healing is applied by a positive amount.
void ChangeHealth(int amount);
public class Enemy : MonoBehaviour
public int damage = 5;
void OnTriggerEnter(Collider other)
IDamageable damageable = other.GetComponent<IDamageable>();
if(damageable != null)
Remember to put both MonoBehaviour classes in their own script files called “Health.cs” and “Enemy.cs”. The interface can be defined in its own file as well, if you want, or leave it in Health. I assume you know how collider triggers work (the “isTrigger” option on the collider, and that one of two colliders must have a rigidbody attached).
Finally, I recommend using integers for health and not floats. Ints are much easier to compare (e.g. apply a buff only if at full health). If you want more resolution, you can just let the range go from 0 - 10000 and then convert down to 0 - 100 resolution for UI display. Games like Diablo model their characters health as integers from zero to a couple of million health points.