How to add delay before running a code in unity

I am trying to make a game that there is a stone that I pick up and throw and i want to add delay between each pick up of the stone

        #region Throwing
        


        if (gunned == true)
        {
            ColOBJ.transform.position = hand.position;
            if (Input.GetButtonDown("Fire1"))
            {
                print("shot");
                gunned = false;
                ColOBJ.gameObject.GetComponent<Rigidbody>().velocity = Vector3.zero;
                ColOBJ.gameObject.GetComponent<Rigidbody>().AddForce(transform.forward* stoneThrowPower);
                ColOBJ.gameObject.GetComponent<BoxCollider>().enabled = true;
                ColOBJ.gameObject.GetComponent<Rigidbody>().useGravity = true;

            }
        }
        #endregion

Introduce a field that contains the next time pickup is possible again, and another field that defines how long the delay is.

[SerializeField] private float pickupDelayTime;
private float pickupAllowedAgainTime;

Then do the following:

if (Time.time >= pickupAllowedAgainTime)
{
    pickupAllowedAgainTime = Time.time + pickupDelayTime;
    // do the pickup
}

Also you should start using variables for readability and performance reasons:

var colBody = ColOBJ.GetComponent<Rigidbody>();
colBody.velocity = Vector3.zero;
colBody.AddForce(transform.forward * stoneThrowPower);
colBody.useGravity = true;

var colCollider = ColOBJ.GetComponent<BoxCollider>();
colCollider.enabled = true;

I assume colOBJ is already a GameObject type, so accessing its .gameObject property is superfluous.

Calling GetComponent all over again is not just making the CPU do the same thing over and over again, needlessly, it also adds a whole lot of junk to each line that you need to process (filter out) mentally. Plus this kind of style has a tendency to hide errors, typically simple type/typo mistakes.

If you got this code from some kind of tutorial, feel free to give them a good old fashioned wristslap from me. Thank you. We really need teachers to use appropriately lean code styles in learning materials rather than spreading cargo cult copypasta. :wink: