I'm learning the basics of combat system by doing it myself, but there is a logical problem I can't figure out. It's on Unity2D, using C#.

Collider2D[] enemiesToDamage = Physics2D.OverlapCircleAll(attackPosition.position, attackRange, attackableLayer);
            for (int i = 0; i < enemiesToDamage.Length; i++)

This is what my attack function looks like, this script is assigned to every enemy in my game and functions properly. But the problem is I want to assign every enemy a unique function whenever they take damage, like jumping back or flying away, and I can't do it in one script. What do I do?

  • 3
    \$\begingroup\$ Hello and welcome to the Game Development stack exchange. Based on your question, I think you would benefit greatly from taking some time to research topics such as inheritance, polymorphism, and overriding. At the very least, you should head over to the Unity Tutorials Page and watch videos "06 Inheritance" and "09 Overriding". This will help you understand how to implement your various enemies without having to copy paste the TakeDamage code in all of their scripts. \$\endgroup\$ – Alex Myers Mar 1 '19 at 22:23
  • 2
    \$\begingroup\$ In short, you will want to create a separate script for each enemy that requires unique behavior, and have those inherit from the base Enemy class. The base Enemy class will contain a virtual TakeDamage method that you will override in the child classes. This way you can have a single TakeDamage method for each type of enemy that will first decrease their health as you have already done, and then perform an action that is unique to each individual enemy type. \$\endgroup\$ – Alex Myers Mar 1 '19 at 22:28
  • \$\begingroup\$ @AlexMyers this looks to me like it could be made into an upvote-worthy answer. :) \$\endgroup\$ – DMGregory Mar 1 '19 at 23:26
  • 1
    \$\begingroup\$ Hey Alex, thanks a lot for warm welcoming and helping me out. I implemented it and it works! \$\endgroup\$ – E. Kinglet Mar 1 '19 at 23:36

As another tool in your toolbox, in addition to the polymorphism Alex Myers recommends in the comments, you can try setting this up in an event-driven manner:

public class DamageTaker : MonoBehaviour {

    public UnityEvent onDamage;
    public float health;

    public void TakeDamage(float damage) {
        health -= damage;

        if(health <= 0f) {
            // TODO: Handle death.

This will give you a widget in the Inspector (or an access point in code) where you can wire up no, one, or multiple methods to call when this object takes damage. These methods can be on the same script or different scripts, or even on different objects in your scene. You can even configure the event to pass the amount/type of damage taken, or a reference to the damage source, so your characters can react accordingly.

With a strategy like this, you can have just one simple damage-handling script that does just that one job (single-responsibility principle), rather than entangling AI behaviour logic in your damage taking code.

Your AI behaviour script(s) remain separate, and can be freely mixed-and-matched with this damage taking behaviour with zero code changes. In data, you can configure which AI behaviour(s) each enemy prefab should use, and which reaction method(s) should be called when it takes damage.

This is an example of the principle of composition over inheritance, allowing you to combine small modular components of code in remixable ways to produce lots of variety without hard-coding each combination, and without deep & inflexible inheritance trees. It's especially useful if you have non-programmers on your team, like game designers and level designers, because it empowers them to make and tune their own combinations without dependencies on code changes.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.