How to Call Attack Function every X seconds, problems with Update Function?

Hi all, hope you can help. I’ve been trawling through the Unity API and these forums for an answer but no luck so far. I’m relatively knew to coding so please bear with me.

I have an enemy that starts to follow and then attack my character once he gets within a certain range (attackDist). Once this happens, I have an attack function called that takes away health from my character every X seconds. What happens in the below code is the attack function keeps getting called so the health shoots down as opposed to a steady chunk of health being lost every few seconds.

I’ve tried things like InvokeRepeating and below I have the standard if statements, but neither are working. I’ve also used triggers instead of playerDist to call the attack function but no luck there either.
I assume it’s something to do with the update function and how it works, but I’m just confusing myself at this stage.

Any ideas?

 function Update () {
    
    	playerDist = Vector3.Distance(transform.position , player.position);
    	
    	if(playerDist <= attackDist){
    	        
    
    	
    	   	transform.LookAt(player);
    		animation.CrossFade ("attack");
    		
    		Attack();
    	        
    	 	return;
    	        
    	}
    
    
    	if(playerDist <= seekplayerDist){
    	
    		transform.LookAt(player);
    		transform.position += transform.forward * speed * Time.deltaTime;		
    		animation.CrossFade ("run");
    		
    		
    	} else {
    
    		animation.CrossFade ("idle");
    		
    	}
    	
    	
    }
    
    
    
    
    
    function Attack (){
    
    var attackRate : int = 5;
    var nextAttack : int = 0;
    
    attackStrenght = weaponDamage + strenght;
    damage = (opponentAC) - (attackStrenght);
    weaponDamage = Random.Range(1.0, 6.0);
    		
    		
    		
    	if (Time.time > nextAttack) {
    	
    	        nextAttack = Time.time + attackRate;
    			//playerStats.health += damage;
    		
    			Debug.Log("Attack Function Called");
 	
    	}
    		
    }

Move these two line to the top of the script (outside any function):

var attackRate : int = 5;
var nextAttack : int = 0;