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 Larcondos · Mar 11, 2018 at 07:24 AM · coroutinetimerwaitforseconds

How to get Coroutine's remaining time in WaitForSeconds?

Hey everyone!

So my issue is that for my invincibility powerup that I am currently using, I have a coroutine which works as follows:

 IEnumerator InvincibleActivate() {
         if (!PC.GetInvincible()) { // If not invincible already, become invincible.
             PC.SetInvincible (true);
             yield return new WaitForSeconds (10);
             PC.SetInvincible (false);
         }
     }


The only problem with this, is that it's possible for two "invincibility" powerups to spawn at the same time. As such, a user could pick up two powerups, but would still only be invincible for 10 seconds.

Is there any way that I can get the remaining time left in the "waitforseconds" and add it to a new counter? Or am I going about this particular problem completely wrong?

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

2 Replies

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

Answer by Bunny83 · Mar 11, 2018 at 08:42 AM

You can't this way since WaitForSeconds is handled internally by the scheduler. If you want a progress you want to do something like:

 float invincibleTimeLeft;
 
 IEnumerator InvincibleActivate() {
     if (!PC.GetInvincible()) { // If not invincible already, become invincible.
         PC.SetInvincible (true);
         for(invincibleTimeLeft = 10; invincibleTimeLeft > 0; invincibleTimeLeft -= Time.deltaTime)
             yield return null;
         PC.SetInvincible (false);
     }
  }
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 Larcondos · Mar 11, 2018 at 03:43 PM 0
Share

Thanks a lot! Also adding for anyone else who might have a similar situation, to add the additional time (So if I was at 4 seconds and activated again, I would want 14 seconds), I simply added a line right before this code.

 float invincibleTimeLeft;
 
     IEnumerator InvincibleActivate() {
         if (PC.GetInvincible ()) {
             invincibleTimeLeft += 10;
         }
         if (!PC.GetInvincible()) { // If not invincible already, become invincible.
             PC.SetInvincible (true);
             for (invincibleTimeLeft = 10; invincibleTimeLeft > 0; invincibleTimeLeft -= Time.deltaTime) {
                 print ("I'm invincible! " + invincibleTimeLeft);
                 yield return null;
             }
             PC.SetInvincible (false);
         }
     }




avatar image K0ST4S Larcondos · Apr 23, 2019 at 01:05 PM 0
Share

What if I have more than 1 type of power ups? xd

avatar image Larcondos K0ST4S · Apr 29, 2019 at 07:40 PM 0
Share

@UnityCosta Well in that case you can simply run different CoRoutines. So this coroutine is for invincibility, but you could have another coroutine that does more or less the same thing, but with some other powerup. $$anonymous$$ake sure you @Larcondos If you want me to see your reply!

avatar image
1

Answer by hexagonius · Mar 11, 2018 at 10:12 AM

You could also check CustomYieldInstructions. you could rewrite a waitforseconds based on that but with an additional time remaining getter.

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 Bunny83 · Mar 11, 2018 at 10:46 AM 0
Share

Right, but be aware that this would add additional overhead. CustomYieldInstructions do start a new nested coroutine. To get access to the time variable you have to store it somewhere outside the coroutine. So just using a simply loop inside the coroutine is the most efficient. Also be careful when you try to "reuse" custom yield instructions. You can not use the same instance twice at the same time. Also you would have to take care of "resetting" the instance before reuse. It's usually meant to be recreated for each use. But as i said even when you reuse the custom yield instruction instance you will create a new coroutine each time you "yield" your instance.

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

84 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

Related Questions

Making a timer using WaitForSecondsRealtime without keyword 'new'? 1 Answer

C# simple delay execution without coroutine? 2 Answers

SpeedBoost won't reset : Problem with either WaitForSeconds or Coroutine (Solved) 2 Answers

How to make WaitForSeconds work as a repeating/looping timer? 1 Answer

Coroutines WaitForSeconds() won't work,WaitForSeconds() isn't doing anything 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