Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
This question was closed Jul 02, 2015 at 01:11 PM by fafase for the following reason:

The question is answered, right answer was accepted

avatar image
1
Question by awplays49 · Jun 30, 2015 at 12:53 AM · ienumerator

Why is it that when I pass this variable to an IEnumerator, it stops working?

These lines

 Vector3 direction = Vector3.up / 4 + Vector3.left / 4;
 transform.Translate (direction);

are changed to

 Vector3 direction = Vector3.up / 4 + Vector3.left / 4;
 StepUp (direction);

Currently.

In StepUp:

 IEnumerator StepUp (Vector3 direction) {
      yield return new WaitForSeconds (0.1f);
      transform.Translate (direction);
 }

Whole code:

 void WalkUpBlocks () {
         Bounds bounds = colliderRef.bounds;
         RaycastHit2D hitLeft = Physics2D.Raycast (new Vector2 (bounds.min.x - 0.125f, bounds.min.y + 0.875f), Vector2.down, 0.75f, layerMask);
         if (hitLeft == true)
         {    
             if (hitLeft.distance == 0.625f)
             {    
                 if (Input.GetKey (KeyCode.A))
                 {    
                     Vector3 direction = Vector3.up / 4 + Vector3.left / 4;
                     transform.Translate (direction);
                 }
             }
         }
         RaycastHit2D hitRight = Physics2D.Raycast (new Vector2 (bounds.max.x + 0.125f, bounds.min.y + 0.875f), Vector2.down, 0.75f, layerMask);
         if (hitRight == true)
         {
             if (hitRight.distance == 0.625f)
             {
                 if (Input.GetKey (KeyCode.D))
                 {    
                     Vector3 direction = Vector3.up / 4 + Vector3.right / 4;\
                     transform.Translate (direction);
                 }
             }
         }
     }
     
     public void CheckForGround (Vector3 velocity) {
         for (int r = 0; r < rayCountY; r ++)
         {
             Vector2 rayOrigin = raycastOrigins.bottomLeft;
             rayOrigin += Vector2.right * (spacingX * r + velocity.x);
             RaycastHit2D groundCheck = Physics2D.Raycast (rayOrigin, Vector2.down, skinWidth, layerMask);
             isJumping = !groundCheck;
         }
     }

Even if I make the method WalkUpBlocks an IEnumerator for simplicity's sake, it doesn't work. Basically I have a left raycast and a right. the right works correctly with coroutines, whereas left raycast stops working with coroutines. This could be that right raycasting comes after, but what is weird is that I Debugged everywhere, and the first place to go wrong is right at the beginning of the coroutine. It definitely is making it to the part where it calls the IEnumerator, but after it is in the coroutine it doesn't Debug?

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
1
Best Answer

Answer by zach-r-d · Jun 30, 2015 at 01:11 AM

Coroutines in C# must be started with StartCoroutine like so:

 StartCoroutine(StepUp (direction));

Calling the method directly, without wrapping it in StartCoroutine or saving it to a variable to later be passed to StartCoroutine, will yield an IEumerator which is simply discarded. Since MoveNext is never called on the returned IEnumerator (under the hood, Unity calls MoveNext on IEnumerators when it is time for coroutines to resume), the yield return inside the function is never passed.

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 awplays49 · Jun 30, 2015 at 04:10 PM 1
Share

cant believe i missed that! sometimes we all need a second pair of eyes :)

avatar image zach-r-d · Jul 02, 2015 at 12:50 PM 0
Share

Glad it worked out. Remember to accept answers if you feel your question has been answered; you gain reputation by doing so. :)

Follow this Question

Answers Answers and Comments

22 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

Related Questions

how do i make my spawn system spawn every 2 seconds then wait 10minutes? 3 Answers

Play AudioClips from a List by using yield WaitForSeconds (C#) 1 Answer

Waiting twice inside coroutine (C#) 2 Answers

yield return M() vs yield return StartCoroutine 2 Answers

How do Coroutines behave with Triggers? 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