Cooldown for attack button

Hi everybody

I have a really big problem with my game. I want to make a script, attached to my GUI object containing all the other GUI, which will deal damage to the enemy when clicked on the button it creates, but after clicking it, you need to wait 3 seconds before you can deal damage again. So I tried doing it with this script, made in the same way as I saw in a tutorial.

var distance = 5;
var mytransform : Transform;
var othertransform : Transform;
var canAttack : boolean = true;

function OnGUI (){
   if (GUI.Button (Rect(0,500,100,100),"ATTACK!") && 
    Vector3.Distance(mytransform.position, othertransform.position) < distance &&
    EnemyProximity.enemyHealth > 0 && canAttack == true){
       Attack();
   }
}

function Attack(){
   canAttack = false;
   EnemyProximity.enemyhealth -= 50;
   yield WaitForSeconds(5);
   canAttack = true;
}

In this script, distance is the distance needed to attack the enemy, mytransform is the transform of the player and othertransform the transform of the enemy. In the inspector I assigned the transforms. When I click the button, no errors appear, but the enemy, in whose script I told it to destroy itself when its health reaches 0, stays. This is the script of the enemy :

static var aggroDistance = 10;
var mytransform : Transform;
var othertransform : Transform;
var glitchCorrector : boolean = true;
var tickerInterval : boolean = false;
var tickerRate = 5.0;
private var nextTick = 0.0;
static var enemyHealth : int = 100;

function Update () {
    if (Vector3.Distance(mytransform.position, othertransform.position)
     < aggroDistance && glitchCorrector == true){
         HealthScript.curhealth += 10;
         glitchCorrector = false;
    }

    if (Vector3.Distance(mytransform.position, othertransform.position) > 
     aggroDistance && glitchCorrector == false){
         glitchCorrector = true;
    }

    if(Vector3.Distance(mytransform.position, othertransform.position) <
     aggroDistance && tickerInterval == false && HealthScript.curhealth > 0){
         HealthScript.curhealth -= 10;
         tickerInterval = true;
    }

    if(tickerInterval == true && Time.time > nextTick){
         nextTick = Time.time + tickerRate;
         tickerInterval = false;
    }

    if(enemyHealth <= 0){
        Destroy(gameObject);
    }
}

Please help me to solve this problem.

I’m not sure exactly what you’re asking as there seems to be two different questions but to have a cooldown you simply need to:

private var lastTimeShot : float;

function Attack() {
    if(Time.time - lastTimeShot > 3) {  // cooldown of 3 seconds
        /* do attack */

        lastTimeShot = Time.time;
    }
}

When you decrement the health on the static variable you are decrementing the global value at EnemyProximity. When you call

EnemyProximity.enemyHealth -= 50;  

You aren’t decrementing the health on the game object. You need to have a reference to the game object and then use GetComponent to get the script. If you know your coroutine is working and that health is being decremented than this is the problem. Stick away from using global variables when you are going to modify them later.