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 OctoMan · Oct 05, 2014 at 11:59 PM · ienumerator

Updating waitforseconds in IEnumerator

I want to update the waitforseconds in runtime, is this somehow possible without restarting the IEnumerator? Or is there another way to get this to work?

 void Update()
     {
         animationwait = (1 / jpm) * 60;//this is infinity at start so the coroutine stops and decreases later on when increasing jpm
             if (jpm > 0 && !CoroutineIsRunning) {StartCoroutine("Animations");}

             if(lastanimationwait != animationwait)//to update the coroutine
     {
         StopCoroutine("Animations");
         CoroutineIsRunning = false;
         lastanimationwait = animationwait;
         StartCoroutine("Animations");
     }
 }
     public IEnumerator Animations()
         {
             while(true)
             {
                 CoroutineIsRunning = true;
                 yield return new WaitForSeconds(animationwait);
                 // change animations later with skills and stuff;
                 babe.animation["Idle"].speed = 1.0f;
                 babe.animation.Play ("Idle");    
             }
         }



I hope you can help me with that. Thanks in advance.

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

· Add your reply
  • Sort: 
avatar image
2

Answer by robertbu · Oct 06, 2014 at 12:53 AM

If you are talking after the WaitForSeconds() is complete, then yes it will pickup the new value the next time it loops through. If you want to interrupt or reduce it during the WaitForSeconds, then you could replace your WaitForSeconds code and create another class instance variable called 'waitTime'. Then you could:

  waitTime = animationwait;
  while (waitTime > 0.0f) {
     waitTime -= Time.deltaTime;
     yield return null;
  }

Then if you change 'waitTime', it will be immediately reflected in the amount of time to wait.

Comment
Add comment · Show 8 · 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 OctoMan · Oct 06, 2014 at 02:17 AM 0
Share

Yes i want to interrupt the WaitforSeconds and change the amount in runtime. After those seconds as you have seen i want to play my animation. When i use your codepiece my animation keeps repeating because waitTime is > 0.0f the whole time.

If i do if(waitime == 0) { babe.animation["Idle"].speed = 1.0f; babe.animation.Play ("Idle"); } in the coroutine it wont play my animation and also stops the coroutine at -0.007163752. Any idea how to fix it?

avatar image robertbu · Oct 06, 2014 at 02:20 AM 0
Share

I need to see your revised code. There is no way that waitTime can remain greater than 0.0 unless Time.timeScale is 0.0 because of this line:

   waitTime -= Time.deltaTime;

Note that the above code replaces this line:

   yield return new WaitForSeconds(animation wait);


And 'waitTime' needs to be declared at the class level, not within the Animations coroutine. And changing 'waitTime' only effects the current cycle through the while() loop. You also have to change 'animationwait' if you want a different wait in next cycle.

avatar image OctoMan · Oct 06, 2014 at 02:51 AM 0
Share

waitTime decreases because of -= Time.deltatime. Time.timescale has nothing to do with it.

I change... animationwait = (1 / jpm) * 60; ...when i buy an item. Items increasing jpm. So animationwait decreases the more items i buy. waitTime = animationwait is setting it to 600 if you buy the first item.

After 600 seconds the animation should play and the timer should begin again. If i update animationwait because i bought another item, it should needs less time until playing the animation.

I hope you know what i mean.

avatar image robertbu · Oct 06, 2014 at 01:24 PM 1
Share

Your code is not what I was suggesting. In your original code, you would replace the single WaitForSeconds() line I specified with the code I specified. You will have nested while() loops.

 void Update()
 {
     animationwait = (1 / jpm) * 60;//this is infinity at start so the coroutine stops and decreases later on when increasing jpm
         if (jpm > 0 && !CoroutineIsRunning) {StartCoroutine("Animations");}
 
         if(lastanimationwait != animationwait)//to update the coroutine
     {
         StopCoroutine("Animations");
         CoroutineIsRunning = false;
         lastanimationwait = animationwait;
         StartCoroutine("Animations");
     }
 }
 public IEnumerator Animations()
     {
     if (animationwait <= 0.0f) {
         Debug.Log ("animationwait need to be greater than 0");
         yield break;
     }
         while(true)
         {
             CoroutineIsRunning = true;
             waitTime = animationwait;
             while (waitTime > 0.0f) {
                  waitTime -= Time.deltaTime;
                  yield return null;
             }
             // change animations later with skills and stuff;
             babe.animation["Idle"].speed = 1.0f;
             babe.animation.Play ("Idle");   
         }
     }
avatar image Bunny83 · Oct 06, 2014 at 01:40 PM 1
Share

@robertbu:
Note: if animationwait is set to 0 or a negative value your application will crash. Here it would be a good idea to implement a safety check or at least an additional yield return null; inside the outer loop. A crash is the worst what can happen to your application as it will take down the editor as well.

Show more comments

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

2 People are following this question.

avatar image avatar image

Related Questions

How Return Or Restart A Coroutine When A Variable Increase? 1 Answer

How to use Coroutines in C++/CLI? 1 Answer

Instantiate Particles, WaitForSeconds and continue 1 Answer

Execute IEnumerator In 1 Frame 4 Answers

Fire rate for gun script, c# (not u/s) 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