How do I make a 5 seconds delay before running the next line of code.

Everything I try gives me errors.

    void Shoot()
    {
        //delay here
        GameObject bullet = Instantiate(bulletPrefab, firePoint.position, firePoint.rotation);
        Rigidbody2D rb = bullet.GetComponent<Rigidbody2D>();
        rb.AddForce(firePoint.up * bulletForce, ForceMode2D.Impulse);
        this.RandomAttack();
    }

Hi.

“Everything I try gives me errors.”

I wonder what you actually tried. Also writing a proper question is usually helpful. Now one must guess what the exact problem is. What is “the next line of code” in this case? You didn’t mention that in your question… I guess you wan to delay the RandomAttack call?

If you bothered to google “unity delay before running the next line of code” - you would find several examples how to do such things.

But in any case, you could do either of these:

  • Use some timer variable, add to it every frame and when it is full, proceed
  • Learn to use coroutines, and yield to a WaitForSeconds with value 5

And BTW, IMO this question belongs to scripting forum, this question has nothing to do with 2D features of Unity.

I use my CallAfterDelay class for delayed action.

See usage notes at bottom below gist code.

1 Like

This is my first time posting so I am sorry about any of that, ik this has nothing to do with 2d but my project is 2d I chose this. I googled “how to add a delay in unity” and watched the first 4 or 5 videos and none of them worked. I also checked out multiple forum posts and none of those worked either. I think the issue might be local as it seems to work for others and the error messages never make any sense. I tried using WaitForSeconds and coroutines, but IEnumerator always gives me errors such as “Error CS519: Invalid token ‘{’ in class, struct, or interface declaration”, the error is refering to the following code. I have added an exclamation mark to exactly where the error is originating.

    IEnumerator AttackDelay();
    !{
        yield return new WaitForSecondsRealtime(5);
        this.Shoot();
    }

Thanks for any help you can offer.

I also did specify what I wanted to delay, in the code “//delay here” was included, I hoped that that would be enough.

1 Like

@Xematical

You have a typing error in your code: IEnumerator AttackDelay();

Don’t add that “;” character, it ends a line.

“I also did specify what I wanted to delay, in the code “//delay here” was included, I hoped that that would be enough.”

OK, I somehow suspected that shooting a bullet and calling the RandomAttack could be two different things… but as I said, writing a proper question would have helped.

Here is an example, where coroutine is called, first Debug.Log is called immediately, then after five seconds the second Debug.Log gets called. Of course, there is no need to do anything before the wait (in your case), I simply added it to illustrate how coroutine could be used.

public class DelayBetween : MonoBehaviour
{
    void Start()
    {
        // start the coroutine
        StartCoroutine(DoSomething(5));
    }

    IEnumerator DoSomething(float duration)
    {
        // do something before
        Debug.Log("Before");

        // waits here
        yield return new WaitForSeconds(duration);

        // do something after
        Debug.Log("After");
    }
}

Please actually don’t use a frame update, this will cause a device running at 30 fps to fill the variable in less than a second