How do I add a delay to my code

When my player attacks, I want it to be indicated, this is done by changing the colour of the object to yellow then after a short delay is turned to red and the attack function is called.

When using Thread.Sleep() or WaitForSeconds(), It waits the total amount of time before changing the colour meaning the player only sees the object turn red once the attack is finished.

I can upload my code if there are any questions.

1 Answer

1

It sounds like you want to change the color, wait for some time, then change the color again and call the attack function. Is that correct? That should be possible by adding the wait after changing color the first time.

For example:

private void AttackTriggered()
{
    StartCoroutine(DoAttack());
}

IEnumerator DoAttack()
{
    mySprite.color = Color.yellow;
    yield return new WaitForSeconds(0.25f);
    mySprite.color = Color.red;
    Attack();
}

In my orignal testing added a wait between changing the color's (which resulted in the program waiting but skipping the instruction of changing to the color yellow and skipping to red. although I did not think about putting the color changing section inside of a coroutine. I will test this is a bit, thanks so much.