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
1
Question by IMTRIGGERHAPPY9 · Oct 22, 2011 at 09:33 PM · loopfor

For loop going based off of Time.deltatime?

how would i make my for loop go based of of seconds?

for example i did this but its not turning out right ( i am trying to transform a button to a certain point on the screen from a previous point)

 var monsterOneRight : float = 100.0;
 var monsterTwoRight : float = 150.0;
 
 var moveRight_Speed : float = 2.0;
 var actualRight : float;
 
 var towerOneLeft : float = 100.0;
 var towerTwoLeft : float = 150.0;
 
 var moveLeft_Speed : float = -50.00;
 var actualLeft : float;
 
 var showMonsterButtons : boolean = false;
 var showTowerButtons : boolean = false;
 
 function Update () {
 
     if (showMonsterButtons == true)
         for(var i: float =0; i<=1000; i += (moveRight_Speed * Time.deltaTime))
             Debug.Log(i);
             if(i == monsterOneRight){
                 monsterOneRight = i;
                 }
Comment
Add comment · Show 3
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 Eric5h5 · Oct 22, 2011 at 09:37 PM 0
Share

You're going to have to be a lot more clear about what it is you're asking.

avatar image IMTRIGGERHAPPY9 · Oct 22, 2011 at 09:55 PM 0
Share

ok i updated it sorry.

avatar image IMTRIGGERHAPPY9 · Oct 22, 2011 at 10:33 PM 0
Share

Can this even be done?

1 Reply

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

Answer by aldonaletto · Oct 22, 2011 at 10:40 PM

You're in the right path:

 for (var i:float = 0; i <= rightTransform; i += Time.deltaTime*transformSpeed){
     button1Right = i;
 }

The variable i must be declared as float, and the parenthesis were mismatched (typo?). But the whole idea is right, you can use floats in for instructions as well. I suggest you to change the title to something like "How to use Time.deltaTime in a for loop?".

Comment
Add comment · Show 4 · 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 IMTRIGGERHAPPY9 · Oct 22, 2011 at 11:01 PM 0
Share

it lags my computer so much! is it unconventional to do something like this?

avatar image IMTRIGGERHAPPY9 · Oct 22, 2011 at 11:02 PM 0
Share

it did work though, just after a long time.

avatar image aldonaletto · Oct 22, 2011 at 11:27 PM 0
Share

That's another problem: lengthy loops really slow Unity down. To avoid this problem, you should add a yield instruction inside the loop - it frees Unity until the next frame, when the system resume in the next instruction automatically. Unfortunatelly, yield instructions cannot be used inside Update. A possible solution is to place the lengthy loop with yield in a separated function - something like this:

 function $$anonymous$$oveRight(){
     for (var i:float = 0; i <= rightTransform; i += Time.deltaTime*transformSpeed){
         button1Right = i;
         yield; // free Unity until next frame
     }
 }

The yield instruction makes the whole function a coroutine. A coroutine starts when you call the function, and is suspended when the yield instruction is found; in the next update cycle, execution is resumed by the system right after yield, and goes ahead until another yield is found or the function ends.
Post the whole script in your question - or in a new one - so we can help you to adapt the code.

avatar image IMTRIGGERHAPPY9 · Oct 22, 2011 at 11:37 PM 0
Share

I didn't put all of it i put the parts that are needed for the for loop, because i dont want my script out there ha ha

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Finding nearest object with a certain tag without for loops. 1 Answer

How to make a for loop display inventory correctly? 0 Answers

How can i attach a bullet to a tower 2 Answers

why does "new GameObject" creates 2 emptys 1 Answer

Quick For Loop Problem 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