how to send message every few seconds?

let me explain. i have a raycast and when it find an enemy it starts Attack(); function and the attack function has a sendmessage. my problem is that the raycast detects the enemy a few times a second and the attack becomes savage like. im happy with the raycast being in the update function but i just want to limit sending the message to maby like 1 every few seconds. any help is appreciated. btw im new to unity

Use a timer.

Example of a timer :

var timerMax : float = 1.0;
var timer : float = 0;

function Update()
{
    timer += Time.deltaTime;
    
    if ( timer > timerMax )
    {
        timer = 0;
        Debug.Log( "SendMessage NOW!" );
    }
}

or you can use a coroutine with a waitforsecondes() in it

You can do the following ways:

1. Use InvokeRepeating

void Start () {

InvokeRepeating(“RepeatingFunc”, initialDelay, repeatTime);

}

void RepeatingFunc () {

 Debug.Log("Repeated at time"+Time.time);

}

2. You can use a coroutine

void Start() {

StartCoroutine(RepeatingFunc());

}

IEnumerator RepeatingFunc () {

yield return new WaitForSeconds(repeatTime);

StartCoroutine( RepeatingFunc() );

}

3. Coroutine with infinite loop

void Start() {

StartCoroutine( RepeatingFunc() );

}

IEnumerator RepeatingFunc () {

while(true) {
      //your code here
      yield return new RepeatingFunc(repeatTime);
}

}