Need to WaitForSeconds without using a Coroutine

I have a menu system for pokemon/rpg style combat in my game and I need a way to have the menu unfold in a specfic order but also pause to display text, etc. I tried using an coroutine but I need this to only run once per call. 1

IEnumerator EnemyAttacking ()
	{	enemyIsAttacking = true;
		eaTextGO.SetActive (true);
		eaText.text = "Enemy Is Attacking...1 ";
		yield return new WaitForSeconds (3f);
		enemyG.RollDice ();
		AttackName ();
		eaText.text = "Enemy Used " + enemyMoveName + " ...";
		yield return new WaitForSeconds (3f);
		if (enemyMoveName == "Kick Attack")
		{
			enemyAttack ();
			Debug.Log ("test");
		}
		yield return new WaitForSeconds (3f);
		eaTextGO.SetActive (false);
		stateMachine.AttackMenu ();
		enemyIsAttacking = false;

}

I don’t understand why you can’t use a Coroutine. You said “I tried using an coroutine but I need this to only run once per call” and I don’t fully understand what you mean, but that doesn’t look like a problem related to using coroutines (a coroutine, just like any other function, runs only one time unless you call it again or it’s a recursive function that calls itself).

It looks like you’re starting the coroutine too often and you end up starting multiple instances of the coroutine. How are you starting it? Maybe you’re using it inside the Update function.

If you try to do something similar without coroutines you’ll need some timers, and if you’re currently starting the coroutines multiple times then you might end up restarting timers or getting some other odd behaviour.

When you start a coroutine, the StartCoroutine method returns a Coroutine object. If for some reason you need only one instance of the coroutine running you can do something like this:

private Coroutine coroutine = null;

...

void Update() {
    if (coroutine != null)
        coroutine = StartCoroutine(TestCoroutine());
}

IEnumerator TestCoroutine() {
    ...
    yield return new WaitForSeconds(2f);
    ...
    coroutine = null;
}

But usually if you need to do something like this you should consider moving the code somewhere else, where it’s called only when it’s needed. Maybe a OnClick callback of a button, or some other function not called on every single frame.

You could make a loop that waits for an animation to finish, if that’s what you’re after.

While (animation not finished) { }

Or if you really need to time three seconds exactly, I think you could use the Stopwatch class in C#.

While (elapsed < time) { }

so I found out that the Invoke () class (Video) might work if I can find a way to call a function from another script, follow this link sorry for the confusion