Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Arundir · Nov 28, 2015 at 09:28 AM · c#yieldwaitforseconds

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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;
 }
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Arundir · Dec 01, 2015 at 03:14 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges