Damage Every Second Not Working

Hello,

Now before you complain about me not googling or something.

http://puu.sh/b6fKa/faca2505aa.png

It’s not worked.

function Update () {
	if(alive){
		if(character_detect){
			ai_character.LookAt(character);
			var distance = Vector3.Distance(ai_character.transform.position, character.transform.position);
			if(distance > 5.0){
				ai_character.animation.CrossFade("walk01");
				transform.Translate(Vector3.forward * Time.deltaTime * 3);
			} else {
				ai_character.animation.CrossFade("attack01");
				invoke();
			}
		} else {
			ai_character.animation.CrossFade("idle");
		}
	}
}

function invoke(){
	InvokeRepeating("attack", 3.0, 1.0);
}

function attack(){
	character.SendMessage("ApplyDamage", damage, SendMessageOptions.DontRequireReceiver);
}

The script on my Character object

function ApplyDamage(damage : int){
	health -= damage;
	if(health <= 0){
		die();
	}
}

function die(){
	Destroy(character.gameObject);
	Application.LoadLevel("Death_Screen");
}

Now what my problem is, is not that it’s not apply damage. It’s applying damage. However it’s applying damage every frame. This is getting quite frustrating as I’ve been working on this for 3 hours now and have yet to find a working solution. I’ve tried many different things.

What I want my script to do is Apply damage to my character every 3 seconds if I’m within a certain area. Why the hell is this not working?

Haven’t tested this, but it seems you need an IEnumerator somewhere in the mix. Try this?

function Update () {
	if(alive){
		if(character_detect){
			ai_character.LookAt(character);
			var distance = Vector3.Distance(ai_character.transform.position, character.transform.position);
			if(distance > 5.0){
				ai_character.animation.CrossFade("walk01");
				transform.Translate(Vector3.forward * Time.deltaTime * 3);
			} else {
				ai_character.animation.CrossFade("attack01");
				invoke();
			}
		} else {
			ai_character.animation.CrossFade("idle");
		}
	}
}



IEnumerator invoke(){
		while (character_detect) {
						character.SendMessage ("ApplyDamage", damage, SendMessageOptions.DontRequireReceiver);
						yield return new WaitForSeconds (3.0f);
				}
	
}
}