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 /
  • Help Room /
avatar image
0
Question by x147836 · Oct 02, 2016 at 07:40 AM · c#coroutinestate-machine

Mixed luck stopping a coroutine.

Throwing this one out to the community. I'm in the process of building a state machine for a turn-based rpg battle system, but have had trouble getting one of my coroutines to stop running. I am attempting to instruct the PlayerTurn coroutine to run the PlayerAttack function, which generates the damage score, just once before ceasing. To do this I'm employing a flag called coRunning. When I don't use the NextState function to advance to the next state this seems to work perfectly fine. The damage number outputs once to the console and is not calculated a second time.

When I use the NextState function, however, it outputs 32 times before moving to the next state. That in particular is what I find confusing, it isn't that it isn't working, it's just doing so on a delay. Messing with the WaitForSeconds parameter seems to change the number of times it outputs, but I don't understand why since the NextState function should be sending me into another state in which PlayerTurn cannot run. Just FYI in case of relevance, BattleProgression is being called in the Update function.

 public enum BattleStates {
         START,
         PLAYERTURN,
         ENEMYTURN,
         LOSE,
         WIN
     }
 
     private BattleStates currentState;
     
     void BattleProgression() {
         switch (currentState) {
             case (BattleStates.START):
                 StartCoroutine(BattleStart());
                 break;
             case (BattleStates.PLAYERTURN):
                 StartCoroutine(PlayerTurn());
                 break;
             case (BattleStates.ENEMYTURN):
                 StartCoroutine(EnemyTurn());
                 break;
             case (BattleStates.LOSE):
                 break;
             case (BattleStates.WIN):
                 break;
         }
     }
 
     //establishes a coroutine flag.
     bool coRunning = true;
     
     //Used to advance to the next state once all code is executed and text printed.
     void NextState(BattleStates state) {
         if (newText.textPrinted == newText.textToPrint.Length) {                                  
             currentState = state;
             coRunning = true;
         }
     }
 
     IEnumerator BattleStart() {
         yield return new WaitForSeconds(0.5f);
         newText.textToPrint = "The " + enemyAttackStat.brownFang.name + " approaches you.";       //a + sign is used to join strings, not a &&.  
         if (coRunning == true) {
             StartCoroutine(newText.PrintTextByLetter());
             coRunning = false;
         }
         NextState(BattleStates.PLAYERTURN);
     }
 
     IEnumerator PlayerTurn() {
         yield return new WaitForSeconds(0.5f);
         if (coRunning == true) {
             PlayerAttack();
             Debug.Log(playerTotalDamage);
             coRunning = false;
         }
         NextState(BattleStates.ENEMYTURN);
     }
 
     IEnumerator EnemyTurn() {
         yield return new WaitForSeconds(0.5f);
         if (coRunning == true) {
             EnemyAttack();
             Debug.Log(enemyTotalDamage);
             coRunning = false;
         }
     }

   
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

Answer by doublemax · Oct 02, 2016 at 08:56 AM

During the time you wait 0.5sec in BattleStart(), Update() will run approximately 30 times (at 60 fps) and each time it will start a new Coroutine. That's probably not what you intended.

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 x147836 · Oct 03, 2016 at 04:06 AM 0
Share

It only seems to do that if I use the NextState function at the end of PlayerTurn. If I don't use it, the coRunning flag does its job and shuts off the coroutine, outputting the damage figure only once. I'm confused by this because all the NextState function is doing is moving me into the next state and resetting the coRunning flag. I even found that when I removed the WaitForSeconds from Playerturn and replaced it with a null return I got the exact same behavior, multiple outputs.

I tried moving NextState out of the coroutine and place it in the next line of code in each case after the coroutine, but then it wouldn't advance to the next state at all, which also doesn't make sense to me.

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

Using coroutine and texture array to randomly associate textures? 1 Answer

Update() function keeps running AFTER script is disabled... 1 Answer

IEnumerator instead of LateUpdate in unity 1 Answer

Display multiple Text in a row for a certain time 1 Answer

this.name not working in Start 1 Answer


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