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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by leorickayzen · Aug 08, 2014 at 06:36 PM · timefunction

Unity crashing when function runs

this function is being used to make sure an object waits for a certian number of seconds before it changes position, for some reason whenever this code is run unity crashes ? any ideas why ?

 function wait(waittime : int){
     
     waittime = 0.000;
     
     var initialtime = Time.time;
     
     var finaltime = 0.000;
     
     var deltatime = waittime - 1.000;
     
     while(deltatime<waittime){
             
         finaltime = Time.time;
         deltatime = finaltime - initialtime;
         
     }
     
 }
 

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 Kiwasi · Aug 08, 2014 at 08:30 PM 0
Share

Good advice for beginners is to avoid while loops altogether.

On the other hand that tends to be bad advice for intermediate to advanced users.

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by rutter · Aug 08, 2014 at 06:36 PM

You've created an infinite loop.

Most game engines, including Unity, work something like this:

  1. Advance the game state by one frame

  2. Render game to screen

  3. Rinse and repeat 30-60 times per second

Your scripts are useful in step one. Unity hands you control of the main thread, which is very powerful but also a bit risky if you do something that causes a crash.

Usually, each script should perform some small, quick operations in Update. Anything that involves waiting is usually structured as a coroutine or timer check -- after all, the game needs to keep running, even if your script is waiting!

For example, you could check: is this script suppose to start moving, yet? Okay, then we do nothing during this frame. That's fine. You don't have to make every script do something in every frame.

In your case, you're looping based on Time.time, which is bad for two reasons:

  1. A wait on the main thread will hang your game until it's finished, which is bad.

  2. Because Time.time updates only once per frame, your loop will actually never finish, which is why you're getting a crash.

Comment
Add comment · 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

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

Time Function in Javascript? 1 Answer

A node in a childnode? 1 Answer

Breathing sound effect using timers 1 Answer

Timer running down too quickly 1 Answer

Weird gameplay slow down problem 1 Answer


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