Delay If statement in Update c#

Hello,

I seem to be having some problems after converting a JS to C#, I used a Invoke to get the results i wanted but dont seem to work in c# for some reason, What i want to achieve is for the GetButton to be delayed by what ever value i set publicly, Any ideas suggestions? Thanks.

using UnityEngine;
using System.Collections;

public class RayCastTree : MonoBehaviour {


public int rayLength = 10;
private PlayerControl playerAnim;
public GameObject tree;

	void  Update (){
		RaycastHit hit;
		Vector3 fwd = transform.TransformDirection(Vector3.forward);
	
		if(Physics.Raycast(transform.position, fwd, out hit, rayLength))
		{
			if(hit.collider.gameObject.tag == "Tree")
			{
				tree = (hit.collider.gameObject);
				playerAnim = GameObject.Find("Katana_Anim").GetComponent<PlayerControl>();
			
				if(Input.GetButton("Fire"))
				{
					tree.GetComponent<TreeController>().treeHealth -= 1;
				}
			}
		}
	}
}

maybe something like…

public float FireDelay = 2.0f;
private float fireTime;

private PlayerControl _playerControl;

void Awake()
{
    //  cached here, but it's not even being used...
    _playerControl = GameObject.Find("Katana_Anim").GetComponent<PlayerControl>();\

    fireTime = Time.time + FireDelay;
}

void Update ()
{
    RaycastHit hit;
    Vector3 fwd = transform.TransformDirection(Vector3.forward);

    bool canFire = false;

    if(Physics.Raycast(transform.position, fwd, out hit, rayLength))
    {
        if(hit.collider.gameObject.tag == "Tree")
        {
            tree = hit.collider.gameObject;
            canFire = true;
        }
    }

    if ((fireTime > Time.time) && canFire)
    {
        fireTime = Time.time + FireDelay;

        if(Input.GetButton("Fire"))
        {
            if (tree != null)
            {
                TreeController treeController = tree.GetComponent<TreeController>();

                if (treeController != null)
                {
                    treeController.treeHealth -= 1;
                }
            }
        }
    }
}

EDIT: this is untested = i’m not near my dev box.

You could always use a coroutine to implement a delay without having to check the time passed every single frame within the update:

IEnumerator waitTime()
	{
		while (true)
		{
			yield return new WaitForSeconds(Time);
			FireButton.SetActive(true);
		}
	}

and then set its active to false when it’s been pressed in the update function.