- Home /
WaitForSeconds while having an Update/CoUpdate in C#
Hello.
I've been looking around for quite a while for this topic, and I know it's almost overdone but still I cannot find an answer that solves my problem.
I am working on a small project that is supposed to be a match3 hybrid game and I'm trying to make the game stop for a couple of secods after a player has made a match so I can play an animation on the player's character and have the enemy character play his own animation before the player moves again. The code I have needs to use the Update() so it can constantly check for matches as well as move the "gems" the player has selected. (I know that my code is not the best and that this could probably be done in much more efficient ways, but I'm really pressed by time and I need to make it work ASAP. I'll work on refining it after I get a "playable alpha", so to speak).
I came to realize that the problem why my game wouldn't stop was the Update function. So I went on and created a CoUpdate() since I read that this should solve the issue, but it didn't and I really don't know what else to do. So, I turn to you guys... I can't take any more for today so I'm hoping that I'll get a hint until I get back to work on that. Here's a pseudocode of how things work right now:
void Start()
{
//do start stuff
this.StartCoroutine(this.CoUpdate());
}
private IEnumerator CoUpdate()
{
while(true)
{
//check stuff and do things every frame
yield return null;
}
}
public void aFunction()
{
//triggered by the player, called from another script
//turns triggers on that activate the things that happen in CoUpdate
//needs to make the game wait after is done
StartCoroutine(WaitForAnimation());
}
IEnumerator WaitForAnimation ( )
{
//triggers the animation from the player's animator script
//should make the game wait until the animation is over
yield return new WaitForSeconds(3);
}
Thank you for your time.
Answer by OncaLupe · Nov 28, 2015 at 07:42 PM
Based on the pseudocode, I don't see why you're using CoUpdate. It's basically doing the same as Update with extra overhead of the coroutine wrapper. If I'm not misunderstanding what you're trying to do, you just need a bool set while the animations are running, and skip the logic inside Update when it's true.
bool isPlayingAnimation = false;
void Update ()
{
//Anything you need doing every frame, even during animations
if(isPlayingAnimation)
return;
//Normal gameplay stuff
}
public void aFunction ()
{
//Stuff
StartCoroutine(WaitForAnimation());
}
IEnumerator WaitForAnimation ()
{
isPlayingAnimation = true;
//Do Stuff
yield return new WaitForSeconds(3);
isPlayingAnimation = false;
}
Thanks for the help. I still haven't figured how to make it work properly, but this way at least I've managed to make the game stop for a few seconds. I'll figure it out somehow...
Scratch that! I figured it out. All I had to do was check my arguments a bit and decide where I should use the boolean "isPlayingAnumation". Now it works perfectly. Thanks a bunch!
Your answer
Follow this Question
Related Questions
Is it possible to have a yield manager? 1 Answer
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
C# yield waitforseconds 3 Answers
Waiting between pingpong loops 2 Answers