How do I set a bool true for half a second after button tap?

It should be a simple task but I’m struggling with it and google searches are not helping.

I would like to see a sample C# script performing this. Preferably with coroutines.

UPDATE:
Apparently I did not make my original question clear enough.

  1. The bool starts in the false state.

  2. When the button is tapped, the bool becomes true for a limited time.

  3. When the time limit is reached, the bool returns to false state.
    I am not trying to delay anything. Just have a bool that starts false, become true for a limited time after a button tap.

I would probably do like this (Untested) :

void OnMouseDown()
{
float mytime;
mytime+=Time.deltaTime*0.5;
StartCoroutine ( ToggleBool (true, mytime) );
}

 IEnumerator ToggleBool(bool test, float time)
 {
     yield return new WaitForSeconds(time);
     test = false;
 }

It’s just a rough code, so you probably need to test this.

bool buttonTap;

void buttonClick()

{

StartCoroutine(" waitTime");

}

public IEnumerator waitTime()

{

buttonTap = true;

yield return new waitforseconds(0.5f);

buttonTap = false;

}

make a gameobject and attach the script. Then in your button click event in the hirearchy just drag the gameobject.

Hi,

There are 2 ways to do the same :

1: Invoke ( “MethodName”, delayTime);
e.g. Invoke ("DelayClick",0.5f);
For best practice you can check if(isInvoking("methodName")) before calling method

2 . By Coroutines
e.g.

private bool temp = false; 
//Call this method on the click of the mouse
 void MouseClick(){
       temp = true;
        StartCoroutine(CoroutineName(0.5f)); // lets say  0.5f
        }
        
        IEnumerator CoroutineName(float delayTime){
        yield return new WaitForSeconds ( delayTime) ; 
temp = false
        // your code will go here
        }

itsharshdeep: Your second method doesn’t work for me. I’m trying to have a boolean be true for a limited time, not delay the switching of a boolean (as I understand your code does).