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 /
This question was closed Sep 12, 2018 at 11:50 PM by Slayer3201 for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by Slayer3201 · Sep 10, 2018 at 02:41 AM · coroutineloopwhileturn based

Buggy While loop/Coroutine, which never truly ends?

I'm working on a classic JRPG, beginning with the combat, as that is by far the most tedious and complex mechanically, so after I finish this, the rest should be a breeze. However, I've ran into an issue that I personally find so complex and illogical, and one that I have next to no idea of how to describe.


Basically, in the combat, I'm currently working on a basic attack function, which should only skip the turn currently. I have it set up so a button on the UI, when activated, calls a function to the CombatDirector, which then finds out who's turn it is, and then proceeds with that character's specified action. As it stands, the attack function should work like this:


1. Character turn begins, UI is enabled and player presses the now enabled Attack button.
2. UI disables, game waits for keyboard input. This is where I'll later implement target selection.
3. After input is given, which right now is given through a simple press of the space bar, the turn is over, and the CombatDirector goes onto the next turn.
4. Rinse and repeat.


Simple enough, correct? And as it stands, that works perfectly. However. As soon as the first turn is over, the second turn character has zero need to press the "Attack" button, and instead, I can simply progress the turn again by pressing the space button. This isn't the case with the first turn, as spamming the space bar before I press the Attack button has zero effect. This already defies my code, as input is being checked for by a while loop that disables itself upon first input, and I know the loop disables due to the debug message I hooked up to said loop no longer coming through. I don't need to press the UI button, I can just spam my space bar and the game keeps on going, skipping through turns as much as I press it. This leads me to believe that SOMEHOW, my original While loop never truly ends, despite every sign pointing to it having ended. I have tried everything, from more Debug.Log 's, to forcing the Coroutine housing the While loop to close, to numerous break; statements, but nothing seems to work. I have a feeling that I might have missed something simple, but I am clueless, and I can't find anything on the subject matter. Here is my code, as well as some bonus screenshots of the console, which shows some even more confusing message orders.


     public void StartAllyAttack () { // This function is called by CombatDirector's AllyAttack() function, which is called via button.
         director.ui.gameObject.SetActive(false);
         StartCoroutine(AllyAttack());
     }
     IEnumerator AllyAttack () {
         Debug.Log("IENumerator START");
         bool choosingTarget = true;
         while (choosingTarget) {
             Debug.Log("RUNNING LOOP");
             if (Input.GetKeyDown(KeyCode.Space)) {
                 Debug.Log(gameObject.name + " ATTACKED");
                 choosingTarget = false;
                 director.AdvanceTurn();
             }
             yield return null; // Pauses execution until next frame, to prevent infinite runs in one frame, and a crash.
         }
         Debug.Log("IENumerator END REACHED");
     }


I hit Start, wait for my first turn, and hit the Attack button. Everything goes to plan. This is what the console looks like:


alt text


After that, without pressing the Attack button or anything, I do nothing else other than press the space bar one more time, and the console turns into this:


alt text


Notice the second send of the "IENumerator START" and initial "RUNNING LOOP" messages. (Ignore the main "RUNNING LOOP" spam number changing, these were screenshots from two different plays.) This confuses me majorly, as the coroutine only starts once the UI button is hit, or at least it should only start then.


Don't mind this edit, just bumping this thread up. Bump. Bump.

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

  • Sort: 
avatar image
0
Best Answer

Answer by AndrewSLeake85 · Sep 12, 2018 at 06:08 AM

There isn't nearly enough context here to solve your issue unfortunately. The only thing I can guess from the screenshots and code provided is that hitting the spacebar is registering as a click on the button that calls AllyAttack(), thus starting it over again.

Comment
Add comment · Show 2 · 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 Slayer3201 · Sep 12, 2018 at 11:50 PM 0
Share

...That was literally the only problem. And it was literally the only thing I didn't think of. Welp, rookie mistake, I feel like an idiot now, but thanks for the help! That saved me hours of headache.

avatar image AndrewSLeake85 Slayer3201 · Sep 13, 2018 at 04:05 AM 0
Share

Absolutely , my pleasure.

Follow this Question

Answers Answers and Comments

156 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to change a variable used in a loop for the outside ? 1 Answer

Unity crashes when using while loop 0 Answers

,Crashing in do while loop when it should be exiting 0 Answers

Which one to use for, while or something else? 1 Answer

What is the best way to loop with very short and accurate interval? 0 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