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 UnityCub · Dec 08, 2018 at 08:03 PM · 2dcoroutineplatformerienumeratorinfinite

Problem With Coroutine - Infinite Loop?

I'm making a 2D platformer in which the player can move onto a launch pad and press a combination of buttons to do a long jump. The long jump is executed by multiplying the player's x velocity by a constant when they're in the air, so they travel a greater distance before landing.

In the code below:

  • xmultiply is the constant that the player's x velocity is multiplied by

  • playerLongJumping is a bool that checks whether the player has pressed the required buttons to long jump

  • initialx is the player's position when they first begin to long jump (To prevent glitches, the player must automatically stop long jumping once they reach a certain displacement, 20, from the start position)

The IEnumerator coroutine is called in void Start.

When I enter play mode in the editor, the entire Unity editor freezes up as soon as the player tries to long jump, and I think it might have something to do with an infinite loop in that coroutine. How can I fix it to make it work as intended? Thanks a lot.

 IEnumerator DoLongJump()
     {
         while (3 == 3) // Condition always true
         {
             xmultiply = 1;
             while (playerLongJumping == false)
             {
                 yield return null;
             }
             initialx = player.transform.position.x;
             while (playerLongJumping == true && Mathf.Abs(player.transform.position.x - initialx) < 20f)
             {
                 xmultiply = 2.7f;
             }
         }
     }




Comment
Add comment · Show 3
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 Vega4Life · Dec 08, 2018 at 08:22 PM 0
Share

Why are you forcing the Infinite loop? You are also probably creating 100s of this coroutine. One is created every time you hit the long jump button and this coroutine never exits.

avatar image UnityCub Vega4Life · Dec 08, 2018 at 08:29 PM 0
Share

How does it create hundreds of coroutines if this coroutine is only called once in void Start?

avatar image Vega4Life UnityCub · Dec 08, 2018 at 09:05 PM 0
Share

In that case it doesn't. I wasn't sure if you assigned it to the space bar input (or some other input). Thus every time you hit the spacebar you would do a long jump. :)

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by ecv80 · Dec 08, 2018 at 09:58 PM

It looks like you might not be setting playerLongJumping to false, after you set it to true. So when Mathf.Abs(player.transform.position.x - initialx) is equal or greater than 20, your while(3==3) loop goes to the next iteration but since playerLongJumping is not false and the next conditions aren't ever met again, it keeps looping without yielding ever again.

Personally, I don't like how you're handling any of this. I fail to see why you use loops instead of simple conditions and also why you would want a coroutine running all the time for this use case. But to each their own.

EDIT: Or it might just be that you're never yielding in your playerLongJumping == true && Mathf.Abs(player.transform.position.x - initialx) < 20f while loop as well.

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

197 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 avatar image avatar image

Related Questions

making a 2D character Speak for different lenghts of time 2 Answers

How to prevent holding down a key to infinitely loop an action? 1 Answer

Line of code skips randomly at times? 1 Answer

Upon shooting, Raycast target moves infinitely 1 Answer

What's the most efficient method of implementing an infinite parallax background? 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