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 Justin Warner · Nov 22, 2010 at 12:37 AM · gameobjectmoving

Moving a GameObject freezes Unity (Possible Infinite Loop)

private var savedTime = Time.time;

function awake () { savedTime = Time.time; }

function Update () {

print("Time: "+Time.time + " Saved Time: "+savedTime); while(Time.time - savedTime <= 5) { transform.Translate(Vector3.forward Time.deltaTime -10); } while (Time.time - savedTime >= 5 ) { transform.Translate(Vector3.forward Time.deltaTime -10); } while (Time.time - savedTime > 10) {

 savedTime = Time.time;

} }

I don't get it, does Unity not use while loops, or am I using them 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
1
Best Answer

Answer by Eric5h5 · Nov 22, 2010 at 01:36 AM

All of your while loops are infinite loops. They will run forever since there is no possibility for the exit condition to be true. You have to wait until the next frame for Time.time to increase, but since the loop never exits, the next frame will never happen.

What you're trying to do is not suitable for Update, which runs once every frame and can't be delayed or interrupted. To schedule a series of events, use coroutines instead. Not sure what you're trying to do, but something like this:

function Start () { while (true) { yield Move(-10.0, 5.0); yield Move(10.0, 5.0); } }

function Move (speed : float, time : float) { for (i = 0.0; i < time; i += Time.deltaTime) { transform.Translate(Vector3.forward Time.deltaTime speed); yield; } }

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 Justin Warner · Nov 22, 2010 at 01:43 AM 0
Share

Could you explain this further?

I checked out: http://unity3d.com/support/documentation/ScriptReference/Coroutine.html

But it doesn't make sense to much. Is their anything else I should really check out, or should I just go off of that?

avatar image Eric5h5 · Nov 22, 2010 at 03:59 AM 0
Share

@Justin Warner: also read all the linked pages about Yield. Anyway I added an example.

avatar image
0

Answer by devilkkw · Nov 22, 2010 at 12:41 AM

while(Time.time - savedTime <= 5) and

while (Time.time - savedTime >= 5 )

is not correct,because u have 2 possibili =5 and make error.

try

while(Time.time - savedTime < 5) and

while (Time.time - savedTime >= 5 )

or

while(Time.time - savedTime <= 5) and

while (Time.time - savedTime > 5 )

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 Justin Warner · Nov 22, 2010 at 12:48 AM 0
Share

$$anonymous$$ade sense to me, but no, still freezes up and I have to end it with task manager.

This happened before too with another loop...

avatar image Justin Warner · Nov 22, 2010 at 12:52 AM 0
Share

Just tried a for-loop, and it still crashes...

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

No one has followed this question yet.

Related Questions

how to tell a game object to move between 2 points(A,B) along x axis , right to left, and then move left to right back to its first position ??? 3 Answers

How to make this kind of control ? 2 Answers

Check if a set of objects is moving 2 Answers

Moving a specific gameobject in a list 1 Answer

GameObject moving different in a build than normally 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