Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Equ1l1brist · Jan 20, 2017 at 04:37 PM · crashif-statementscrashingwhile-loop

While loop crashing in update but why not if statement?

Anybody able to tell me why this code immediately crashes when using a while loop, yet works fine with the if statement?

 void Update()
     {//Tracks players position. Script not attached to player object
        
         playerXPos = player.transform.position.x;
         playerZPos = player.transform.position.z;
 
   while (playerZPos > 0f) {
             print(playerZPos);
         }
     }


The player's position can return to below 0.

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

Answer by HenryStrattonFW · Jan 20, 2017 at 07:44 PM

This is because an if statement will test, pass or fail, and then let the update method end meaning the game can continue to process this frame and move on to the next.

However a while loop if playerPosZ is not immediately

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 Equ1l1brist · Jan 21, 2017 at 12:24 PM 0
Share

I think I understand you, but just to make sure. Will a while loop like this one within Update() stop Update() from functioning next frame?

avatar image HenryStrattonFW Equ1l1brist · Jan 21, 2017 at 03:51 PM 1
Share

It will not stop update from function on the next frame as such, since update will never exit (unless the while loop ends) and since your unity code will be running on a single thread, if this update loop does not exit, the current frame never ends and so the next frame never starts (this sort of error is an infinite loop, we all write them, and they look up the unity editor which can be a real pain).

avatar image
1

Answer by DiGiaCom-Tech · Jan 21, 2017 at 03:56 PM

In your case, processing will stay within the WHILE LOOP for as long as the player's Z is greater than 0f. Because this value never changes within the while loop you never exit this while loop.

When your case is met (playerZPos > 0f) processing enters the loop but can never leave because you have not provided a way for it to exit. Unless there is something within the while loop that adjusts the player's Z position (i.e. some decrement like 'playerZPos -= time.deltatime * 1f'). Your loop will continue repeating infinitely as the playerZPos never changes from the value it was when the while loop was entered.

No other processing occurs anywhere else in your application while you are within this loop (other than printing the playerZPos).

While loops are normally used to process lists, arrays, or perform tasks on sets of things. For example, lets say I want to check every X & Y point on an image for something or every inventory item in a list and sum the weights.

To do this we need to increment counters or tests to control when to exit the while loops ...

  // Sudocode Warning
  int X = 0;
  int Y = 0;
  bool FoundSomething = false;
  WHILE (X <= image.width && !FoundSomething) {
      X++;
      Y = 0;
      WHILE (Y <= image.height && !FoundSomething) {
           Y++;
           FoundSomething = LookForSomething(X,Y);
      }
 } 

This will step through every coordinate, both X and Y, in the image and LookForSomething() in it. Note that as soon as X or Y exceed the image width or height those while loops are exited and code processing moves on. Note also that if the LookForSomething() function returns TRUE (e.g. finds what it was looking for) then we can also exit both while loops.

Bottom line is that one needs to be careful when creating while loops to ensure that they have a reasonable exit point and never loop endlessly.

Comment
Add comment · Show 1 · 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 Equ1l1brist · Jan 22, 2017 at 11:40 AM 0
Share

Thanks for the great explanation! Understand the issue perfectly now. Thanks guys

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

93 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

Related Questions

"If statement" and whiled loop acting strange and crashes 1 Answer

Why loops crash the entire OS/PC? 0 Answers

Unity crashes when I make a new project. Please help. 0 Answers

Unity crashes when compiling this script. Where is the problem? 0 Answers

Unity 2017 crashes for no reason 0 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