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.
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();
}
â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");
}
}