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 rsoneriu · Apr 01, 2014 at 08:44 AM · loopdelaywhile

Increment in while loop, with timeout

Hey guys, I did look all over the forums for an answer to this question, but found nothing :( The thing is, I have a function, that should increment a variable (X) in a while loop, something like this while (x<5){ x++; } The only problem is that the loop is carried out instantaneously (as it should), but I want the increment to be done once per frame rate (Time.deltaTime should be used I think), but I don't really know how can I set a delay to each incrementing. Any help would be appreciated! Many thanks

Comment
Add comment · Show 1
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 Questhelp · Apr 01, 2014 at 07:28 PM 0
Share

Try swapping while for if.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by besttof · Apr 01, 2014 at 09:22 AM

You could use a timer variable and update that in the Update function like so:

 public float delay = 1f;
 private float timeout;
 private float x;
 
 void Update() 
 {
     timeout -= Time.deltaTime;
     if (timeout <= 0)
     {
         x++;
         timeout += delay;
     }
 }

But it would probably be easier and more in line with your initial approach to use a coroutine:

 public float delay = 1f;
 
 void Start()
 {
     StartCoroutine(Foo(5));
 }
 
 IEnumerator Foo(int count)
 {
     int x = 0;
     while (x < count)
     {
         x++;
         yield return new WaitForSeconds(delay);
     }
 }

Both examples are C# btw, but should work equally well in UnityScript.

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 rsoneriu · Apr 01, 2014 at 11:03 AM 0
Share

Thanks, I will try your solution when I reach home. the thing is I want everything to happen in the update() function. I am using unity script, but if you say it works, I'll try to add it and see what happens. The thing I need is to make it increment once with the frame rate in update (time.deltatime i think).

avatar image besttof rsoneriu · Apr 01, 2014 at 07:21 PM 0
Share

Hm, reading your question again, I think you might mean something like this:

 public float stepSize = 3f;
 
 void Update()
 {
   if (x < 5) x += Time.deltaTime * stepSize;
 }

That will increment the value of x continuously over time, while my previous example will make x increase in discrete steps (i.e. not continuously). Is that what you wanted to know?

avatar image rsoneriu rsoneriu · Apr 04, 2014 at 05:57 AM 0
Share

Not exactly, but I figured it out. I needed to increment a value if a certain condition is met, once per frame, until it reaches a value. I ended up using a while loop (to stop execution of anything else, thus not needing flags if the action started or not) and a co-routine function that increases the value. Nevertheless, many thanks for your help and effort!

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

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

Problem with while statement. 1 Answer

Timed while loop? 0 Answers

A node in a childnode? 1 Answer

Blender Animation Looping Delay Problem 2 Answers

Audio loop after 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