How can I make my enemy attack me every few seconds?

This is the script that I’m using for the enemy to “attack” my player. What it basically does is that the script looks for whatever collided with my player and checks if it is tagged Enemy, if it is then the player takes damage. The only problem is that the player only takes damage when the enemy enters the trigger. I would like it to be so while the enemy is inside the trigger, it can constantly damage the player every couple of seconds. Thanks for the help and I’ll be clearer if needed.
public const int maxHealth = 100;

public int currentHealth = maxHealth;

void OnTriggerEnter(Collider other)
{
    if (other.gameObject.CompareTag("Enemy"))
    {
        currentHealth -= 10;
        if (currentHealth <= 0)
        {
            currentHealth = 0;
            Destroy(gameObject);
        }
    }
}

public void TakeDamage(int amount)
{
    currentHealth -= amount;
    if (currentHealth <= 0)
    {
        currentHealth = 0;
        Destroy(gameObject);
    }
}

}

use OnTriggerStay instead of OnTriggerEnter because OnTriggerStay is called almost all the frames for every Collider other that is touching the trigger.

This message is sent to the trigger and the collider that touches the trigger. Note that trigger events are only sent if one of the colliders also has a rigidbody attached.

void OnTriggerStay(Collider other)
    {
        if (other.gameObject.CompareTag("Enemy"))
     {
         currentHealth -= 10;
         if (currentHealth <= 0)
         {
             currentHealth = 0;
             Destroy(gameObject);
         }
     }
    }

OnTriggerStay can be a co-routine, simply use the yield statement in the function.

You can call TakeDamage using Invokes the method methodName in time seconds, then repeatedly every repeatRate seconds.

  InvokeRepeating("TakeDamage", 2.0f, 0.3f);

You should read up on coroutines here:

Or search them on the Learn portal. Basically you write a function that returns IEnumerator, and call it with StartCoroutine(yourCoroutine). By using the yield statement, it will get called every x seconds as long as you wish.

The link has som simple examples and a better explanation.

As far as I remember Coroutine doesn’t work correctly if you write inside OnTriggerEnter().