How can I make a variable false for a period of time?

I just started Unity and javascript today, so I basically have no idea what I’m doing. I made an attacking system, but I want to create a delay in between attacks. Here’s what I tried but it said Update can not be coroutine.

#pragma strict

var TheDamage : int = 25;
var Distance : float;
var MaxDistance = 1.5;
var TheMace : Transform;
var inAnim = false;

function Update()
{
	if (Input.GetButtonDown("Fire1") && inAnim == false)
	{
		TheMace.animation.Play("maceAttack");
		var hit : RaycastHit;
		if (Physics.Raycast(transform.position, transform.TransformDirection(Vector3.forward), hit))
		{
			Distance = hit.distance;
			if (Distance < MaxDistance){
				hit.transform.SendMessage("ApplyDamage", TheDamage, SendMessageOptions.DontRequireReceiver);
				inAnim = true;
				yield WaitForSeconds(0.5);
				inAnim = false;
			}
		}
	}
}

Update() function cant be a coroutine. You have to just define a different function to be a coroutine (in this case setAnimationState() ).

#pragma strict
 
var TheDamage : int = 25;
var Distance : float;
var MaxDistance = 1.5;
var TheMace : Transform;
var inAnim = false;
 
function Update()
{
    if (Input.GetButtonDown("Fire1") && inAnim == false)
    {
        TheMace.animation.Play("maceAttack");
        var hit : RaycastHit;
        if (Physics.Raycast(transform.position, transform.TransformDirection(Vector3.forward), hit))
        {
            Distance = hit.distance;
            if (Distance < MaxDistance){
                hit.transform.SendMessage("ApplyDamage", TheDamage, SendMessageOptions.DontRequireReceiver);
                setAnimationState();
            }
        }
    }
}

function setAnimationState() {
    inAnim = true;
    yield WaitForSeconds(0.5);
    inAnim = false;
}

if (Distance < MaxDistance){
hit.transform.SendMessage(“ApplyDamage”, TheDamage, SendMessageOptions.DontRequireReceiver);
Delay();
}

function Delay():void{
    inAnim = true;
    yield WaitForSeconds(2);
    inAnim = false;
}

Consider using getcomponent instead of sendmessage.

Add a variable, for example “delayBetweenAttacks” and set it to your delay value, for example 0.5.
Add another one called “timeSinceLastAttack”.

Then modify Update as follows:

function Update()
{
    timeSinceLastAttack += Time.deltaTime;
    
    if (Input.GetButtonDown("Fire1") && timeSinceLastAttack > delayBetweenAttacks)
    {
        TheMace.animation.Play("maceAttack");
        var hit : RaycastHit;
        if (Physics.Raycast(transform.position, transform.TransformDirection(Vector3.forward), hit))
        {
            Distance = hit.distance;
            if (Distance < MaxDistance){
                hit.transform.SendMessage("ApplyDamage", TheDamage, SendMessageOptions.DontRequireReceiver);
            }
        }
        timeSinceLastAttack = 0;
    }
}