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
1
Question by davve146 · Oct 21, 2019 at 06:12 AM · coroutinecoroutines

I don't understand this coroutine

So basically I'm trying to understand why my coroutine keeps getting called by the update method each frame. I find this weird because I'm turning one of the conditions for Update off when starting the coroutine. If anyone could provide some insight it would be greatly appreciated!

 enum Direction
 {
     North,
     East,
     South,
     West
 }
 
     void Update()
     {
 
         if (!isMoving && isAllowedToMove)
 
         {
             input = new Vector2(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical"));
             if (Mathf.Abs(input.x) > Mathf.Abs(input.y))
                 input.y = 0;
             else
                 input.x = 0;
 
             if (input != Vector2.zero)
             {
 
                 if (input.x < 0)
                 {
                     currentDir = Direction.West;                    
                 }
                 if (input.x > 0)
                 {
                     currentDir = Direction.East;
                 }
                 if (input.y < 0)
                 {
                     currentDir = Direction.South;
                 }
                 if (input.y > 0)
                 {
                     currentDir = Direction.North;
                 }                
 
                 StartCoroutine(ChgDir());
             }
 
         }
 
     }
 
     public IEnumerator ChgDir()
     {
         isMoving = true;
         switch (currentDir)
         {
             case Direction.North:
                 gameObject.GetComponent<SpriteRenderer>().sprite = walk_up_left;
                 yield return new WaitForSeconds(0.1f);
                 gameObject.GetComponent<SpriteRenderer>().sprite = northSprite;
                 break;
             case Direction.East:
                 gameObject.GetComponent<SpriteRenderer>().sprite = walk_right_left;
                 yield return new WaitForSeconds(0.1f);
                 gameObject.GetComponent<SpriteRenderer>().sprite = eastSprite;
                 break;
             case Direction.South:
                 gameObject.GetComponent<SpriteRenderer>().sprite = walk_down_left;
                 yield return new WaitForSeconds(0.1f);
                 gameObject.GetComponent<SpriteRenderer>().sprite = southSprite;
                 break;
             case Direction.West:
                 gameObject.GetComponent<SpriteRenderer>().sprite = walk_left_left;
                 yield return new WaitForSeconds(0.1f);
                 gameObject.GetComponent<SpriteRenderer>().sprite = westSprite;
                 break;
         }       
         isMoving = false;
         yield return 0;
 
     }

Comment
Add comment · Show 2
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 tormentoarmagedoom · Oct 21, 2019 at 01:48 PM 1
Share

Hello.

You do this at the end of the corutine

  is$$anonymous$$oving = false;


So it can be executed again, no? and only waits for 0.1 seocnds, so it is executed 10 times a seconds

avatar image Owen-Reynolds · Oct 30, 2019 at 05:26 PM 1
Share

To follow up on what TA wrote, try increasing the delay. Replace all of those 0.1's with a variable "moveWaitSeconds" or something. Set it to 2.0 (something so huge you can't miss it). Then make sure is$$anonymous$$oving is public in the Inspector -- you should see it uncheck for 2 seconds, then go back on.

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by anilhdas · Oct 29, 2019 at 06:11 AM

TLDR: isAllowedToMove is not seemingly reset!

The boolean isMoving is reset to false at the end of each Coroutine call, which makes the 1st condition in update to be true.

And assuming initial value isAllowedToMove to be true (If not the coroutine wouldn't be called at all), I don't see it being reset to false.

Comment
Add comment · 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
0

Answer by davve146 · Oct 29, 2019 at 09:52 PM

Thanks for the reply @anilhdas .

The thing is that isAllowedToMove is always true unless another method or script changes its value. In other words I always want the update method to be called unless some in-game event changes it or the character is moving ( isMoving = false ). The problem here is that update is called again before the character has stopped moving, resulting in the coroutine running several times instead of once. If I insert a Debug.Log() statement it prints several times over the course of the character moving once.

I’m assuming that the coroutine doesn’t wait long enough for the action to complete but I could be totally wrong about that. Thanks again for the reply

Comment
Add comment · 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

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

195 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 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

Why isn't my coroutine working when I call it from another script. 0 Answers

Coroutine Not working properly. 1 Answer

Coroutines and loading screens 1 Answer

Using Grid-like singletons but have an issue with Coroutines. 2 Answers

"Can't add script behaviour AICharacterControl. The script needs to derive from MonoBehaviour!" ? 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