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 /
  • Help Room /
avatar image
0
Question by wai030 · Mar 17, 2021 at 10:50 AM · waitforsecondsstartcoroutine

how to change a value over time

so i would like to minus a value (i.e. 50) within 0.0166667 second, i have tried startcoroutine in update(not accurate), in while loop (it end in 0 second) and also invoke method in while loop(blow my pc up) . this is the code of the start coroutine in while loop.

  float x = 50, length =  0.0166667, g;
  bool b = true;
  void Update()
  {
       g = Time.time;
       while (x>0)
       {
            StartCoroutine(Tt());
       }
       if(x == 0 && b == true)
       {
            Debug.Log(g);
            b = false;
        }
  }
  IEnumerator void Tt()
   {
       x -= 1;
       yield return new WaitForSeconds(length / 50);
   }
   

so this will give g =0 in debug.log, which is in accurate, and i am not sure whether it is the problem of Time.time or my code.

Comment
Add comment · Show 11
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 Hellium · Mar 17, 2021 at 11:55 AM 0
Share

0.0166667 seconds = 1/60 seconds

Unless your game runs quicker than 60 FPS, you could simply set the value to 0 after one frame.

Otherwise, yes, your code is completely messed up to do what want

avatar image wai030 · Mar 17, 2021 at 12:25 PM 0
Share

but is it possible to do it under 60 fps or at 60 fps?

avatar image Hellium wai030 · Mar 17, 2021 at 12:49 PM 0
Share

It all depends on the performances of your game. Coroutines and the Update method can't be called quicker than once per frame.

avatar image wai030 Hellium · Mar 17, 2021 at 01:28 PM 0
Share

so is that the only method?

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Tulula · Mar 17, 2021 at 05:16 PM

I managed to get a result using the code below. There were a few errors. Float values such as length need 'f' to be placed at the end to signify a float - length = 0.0166667f; IEnumerator does not require void return type, simply IEnumerator Tt() works.

I used a while loop in the enumerator to check if x > 0 and if so the minus 1 and yield.

 float x = 50, length = 0.0166667f, g;
     bool b = true;
 
     void Start()
     {
         StartCoroutine(Tt());
     }
     void Update()
     {
         g = Time.time;
         if (x == 0 && b == true)
         {
             Debug.Log(g);
             b = false;
         }
     }
     IEnumerator Tt()
     {
         while (x > 0)
         {
             x--;
             yield return new WaitForSeconds(length / 50);
         }
     }

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 wai030 · Mar 18, 2021 at 03:41 AM 0
Share

do you mean you can get 0.0166667f when Debug.Log(g); is executed?

avatar image Tulula wai030 · Mar 18, 2021 at 01:29 PM 0
Share

No not quite, what I got was 0.08 something but that is most likely due to the performance of my machine, it is not the best nor worst so there could easily be a delay.

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

158 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

Related Questions

nothing called after WaitForSeconds 1 Answer

Show image then wait for x seconds and hide it again. 0 Answers

Performance Issue with Coroutines 2 Answers

IENumerator does not work 0 Answers

Instantiate an object and destroy it after given time 2 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