How do I make a number countdown in the UI but only when a key on keyboard is held down?

I know it’s simple but I just can’t seem to get it working or find help for this specific thing.

Try something like this…

public float NumberToCountDown = 25;

void Update()
{
    if (Input.GetKey(KeyCode.Space))
        if(NumberToCountDown>0)
        {
             NumberToCountDown-=Time.deltaTime;
             UIObjectToDisplayThisOn.text = ((int)NumberToCountDown).ToString();
        }
}

I wrote this directly in the answer so no guarantees against typos.

You will need to replace UIObjectToDisplayThisOn with a reference to your UI object that will display the number.

Change NumberToCountDown to whatever you want the number to start at, in the inspector.

Basically what this is doing, is reducing the number by Time.deltaTime each update cycle, then casting it into an int for the display so you don’t get all the decimal values displayed. If you want those decimals to display, just get rid of the (int).

Hope this helps,
-Larry