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 /
avatar image
0
Question by thaiscorpion · Jul 22, 2012 at 04:21 PM · javascriptyield

How to stop a yield function?

How can I make the yield restart the timmer every time it is executed or how can I stop it?

I´ve got this code:

 function Grab () {
 
  if ( canGrab ) {
  
  canGrab = false;
  grabbing = true;
  
  //Do stuff
  
  yield WaitForSeconds ( grabDelay );
  
  grabbing = false; 
 
  //Do stuff
  }
 }

When the player jumps canGrab is set to true, when he hits a wall his is held there for grabDelay (approx 2 seconds) this works fine but if the player hits a wall, grabs it and then jumps of it and grabs another one the: yield WaitForSeconds ( grabDelay ) is almost over and the second he grabs the wall the grabDelay reaches 0 and he falls off.

How can I make the yield restart the timmer every time it is executed or how can I stop it?

Any help would be greatly apreciated, thanks!

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

3 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by thaiscorpion · Jul 23, 2012 at 04:01 PM

I dont think its posible to do what I asked. I ended up making a timer that was reduced in the update function and every time grab() is executed the timer is set to the timeDealy.

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 03gramat · Apr 29, 2014 at 03:57 PM 0
Share

I hate to bring up an old post, but if anyone is interested, one way is to cancel the coroutine and the start it again (I made a tutorial on it available here) Hope it helps somebody!

avatar image
1

Answer by ScroodgeM · Jul 22, 2012 at 05:26 PM

instead of just start coroutine you can do the following

StopCoroutine("Grab");
StartCoroutine("Grab");
Comment
Add comment · Show 3 · 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 thaiscorpion · Jul 22, 2012 at 09:45 PM 0
Share

Thanks im going to try that now ;D

avatar image thaiscorpion · Jul 23, 2012 at 04:00 PM 0
Share

This didnt work the yield would still be counting I dont know why

avatar image ScroodgeM · Jul 23, 2012 at 06:09 PM 0
Share

do you start coroutine using string or still using method without quotes? to stop coroutine this way you should start it by method name in string

avatar image
0

Answer by Sener Dude · Apr 11, 2014 at 12:07 PM

Try this hack : If you change "enableYieldFunction" to false before yield start. it will not work.

 public class Test : MonoBehaviour {
 
   internal bool enableYieldFunction = true;
 
   void OnTriggerEnter2D(Collider2D col)
   {
      StartCoroutine(EatFood(col)); // Eat Food
   }
 
  IEnumerator EatFood(Collider2D col)
  {
     yield return new WaitForSeconds(10f); // Wait 10 second
     if (enableYieldFunction)
     {
        // Do your things..
     }
   }

   void Update () {
      if (Input.GetButtonDown("Fire1"))
      {
         enableYieldFunction = false; // Stop Eating Food
      }
   }

}

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

7 People are following this question.

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

Related Questions

Problem with yield waitForSeconds in for loop 0 Answers

Confused about Coroutines 2 Answers

How can I make a variable false for a period of time? 3 Answers

Yield Waitforseconds not working at all 3 Answers

yield works, but yield WaitForSeconds does not? 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