How to make an attack hit multiple times

Basically I’m trying to make a an attack that appears a short distance in front of the player and destroys itself after x time (about 0.5 seconds) and inflicts each instance of damage 0.1 seconds apart for a max of 5 hits for each enemy, so if an enemy enters it late it will still take a few hits but the attack will end before they take their max of 5, but if you get 3 enemies in it for the entire duration they would each take 5 instances of damage 0.1 seconds apart. I’ve got most of it done but I’m struggling to figure out how to inflict the damage the way I’m trying to. I’ll put my code, its probably missing a bit cause I’m in-between ideas.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class RapidAreaSlashHitScript : MonoBehaviour
{

    public int CurrentDamage = 5;
    public int HitAmount = 5;
    public float Dur = .52f;
    private bool HitReady = true;
    private float KnockBack = 0;
    private float MovementDelay = 0.3f;

    // Start is called before the first frame update
    void Start()
    {
        HitAmount = 5;
        StartCoroutine(SelfDestruct());
    }

    private IEnumerator SelfDestruct()
    {
        yield return new WaitForSeconds(Dur);
        Destroy(gameObject, 0f);
    }

    private void OnTriggerStay2D(Collider2D other)
    {
        if (other.CompareTag("Enemy") && HitReady == true)
        {
            HitReady = false;
            StartCoroutine(Hit(other));
            
        }
        else if (HitAmount <= 0)
        {
            Debug.Log("Time to die!");
            Destroy(gameObject, 0f);
        }
    }


    private IEnumerator Hit(Collider2D enemy)
    {
        enemy.GetComponent<ClonableEnemyStats>().TakeDamage(CurrentDamage, KnockBack, MovementDelay);
        
        yield return new WaitForSeconds(0.1f);
        HitAmount--;
        HitReady = true;
    }

}

Hi @Terrafighter, maybe you can try the following modified script?

public class RapidAreaSlashHitScript : MonoBehaviour
{
    public int CurrentDamage = 5;
    public int HitAmount = 5;
    public float HitInterval = 0.1f;
    public float Dur = .52f;
    private float KnockBack = 0;
    private float MovementDelay = 0.3f;
    private List<Collider2D> detectedEnemies = new List<Collider2D>();
    private float lastHitTime = -1;

    private void Start()
    {
        StartCoroutine(SelfDestruct());
        StartCoroutine(UpdateHit());
    }

    private IEnumerator SelfDestruct()
    {
        yield return new WaitForSeconds(Dur);
        Destroy(gameObject, 0f);
    }

    private IEnumerator UpdateHit()
    {
        // Wait until OnTriggerStay2D is invoked
        yield return new WaitForFixedUpdate();

        int currentHitAmount = HitAmount;

        while (true)
        {
            yield return new WaitForFixedUpdate();

            if (Time.time - lastHitTime >= HitInterval)
            {
                foreach (Collider2D enemy in detectedEnemies)
                {
                    Hit(enemy);
                }

                detectedEnemies.Clear();

                lastHitTime = Time.time;

                currentHitAmount--;

                if (currentHitAmount <= 0)
                {
                    Debug.Log("Time to die!");
                    Destroy(gameObject, 0f);
                }
            }
        }
    }

    private void OnTriggerStay2D(Collider2D other)
    {
        if (other.CompareTag("Enemy"))
        {
            if (!detectedEnemies.Contains(other))
            {
                detectedEnemies.Add(other);
            }
        }
    }

    private void Hit(Collider2D enemy)
    {
        enemy.GetComponent<ClonableEnemyStats>().TakeDamage(CurrentDamage, KnockBack, MovementDelay);
    }
}

just learned the times the site gave me an error trying to post, it still posted the question and now I got to delete duplicates…