Shoot-Cooldown

I have a Script, i want it to spawn an object on-click but with a cooldown. Well, my isn’t working…

using UnityEngine;


public class Shield_Activate : MonoBehaviour
{

	public Transform shotPrefab;
	public Transform ParentGameObject;

	public float shootingRate = 10f;
	
	
	
	private float shootCooldown;
	
	void Start()
	{
		shootCooldown = 100f;
	}
	
	void Update()
	{

		if (shootCooldown > 0)
		{
			shootCooldown -= Time.deltaTime;
		}
	}


	public void Attack(bool isEnemy)
	{
		if (CanAttack)
		{
			shootCooldown = shootingRate;
			
			
			var shotTransform = Instantiate(shotPrefab) as Transform;
			
		
			shotTransform.position = transform.position;
			shotTransform.parent = ParentGameObject.transform;
			
			// The is enemy property
			ShotScript shot = shotTransform.gameObject.GetComponent<ShotScript>();
			if (shot != null)
			{
				shot.isEnemyShot = isEnemy;
			}
			
			// Make the weapon shot always towards it
			MoveScript move = shotTransform.gameObject.GetComponent<MoveScript>();
			if (move != null)
			{
				move.direction = this.transform.right; 
			}
		}
	}
	

	public bool CanAttack
	{
		get
		{
			return shootCooldown <= 100f;
		}
	}
}

You never set the shootCooldown back to an initial value …

Another possible solution (you may have to adapt). It was a game were user could throw objects (same concept as shooting bullets).

public class Fighter : MonoBehaviour
{
    public float DelayBetweenThrows = 1;

    float lastThrowDate;

    void Start()
    {
        lastThrowDate = Time.time;
    }

    public void Throw( Vector3 direction )
    {
        
        if ( ( Time.time - lastThrowDate > DelayBetweenThrows ) )
        {
            //Stuff
            lastThrowDate = Time.time;
        }
    }
}