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 Ilhan · Jan 09, 2012 at 06:04 PM · loopwhilefrozen

While Loop won't end when SendMessage was executed..

Hey and thanks for reading my thread. I have a quite short question and (I think) it is related to the mechanics of Unity , so Id like to ask here and hope to find an answer.

I ran into a weird problem. When I send an input (for example I press the escape button), I start StartGrowing() which does the following:

     while (!stop)
     {
         AddSprite(block, Xpos, Ypos);
         Ypos += 16;
     }

Good, now stop is false on start , Xpos is 0, Ypos is -1040 So my little while loop should start adding block sprites (which are 16x16 and contain a collider) one above another (+16 on Y axis). I have set up a wide capsule collider which is located at about Y = 0.

On that collider I have a script that seeks for OnCollisionEnter, and as soon as it is triggered, it does a SendMessage("VerticalHit") to my main script.

I have checked if this works by manually colliding with it and it worked like a charm. This is how VerticalHit() looks like:

     stop = true;

Basically here is what all this above does (or well should do):

"Wait for the user to press escape key" "Set the bool 'Stop' to false" "When escape key is pressed, do StartGrowing()" "StartGrowing() builds blocks one above another until Stop == true; "At the same time, the script inside the collider checks for collision" "If one of the blocks hits the collider, it does a SendMessage("VerticalHit") to the main script" "VerticalHit sets the bool 'Stop' to true; "The while loop in StartGrowing() stops because the criteria (!stop) is not true"

Correct me if I coded an error, but could it be that Unity can't handle a while loop and another script (for example my collider script) at the same time? The above mentioned freezes Unity.. and I have no idea why. As I said, I checked manually by adding block by block if the SendMessage() does what it should do, everything works fine.

Hope I'll find the answer. Thanks in Regards ~Ilhan

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
0
Best Answer

Answer by Eric5h5 · Jan 09, 2012 at 07:07 PM

Unity can handle loops just fine; it's basic logic. The problem is that your while loop is infinite and nothing will stop it from running, therefore Unity freezes, and can't progress past the loop. Setting "stop" to true at some point in the future won't stop the while loop from running forever, because Unity will never get to the point where stop is true. It needs stop to be true while the loop is actually running, inside the loop itself, not outside. A correct loop has a condition that causes it to end:

 var count = 0;
 while (!stop) {
     AddSprite(block, Xpos, Ypos);
     Ypos += 16;
     if (++count == 10) stop = true; // or something
 }

After the loop has finished, then Unity can continue with the rest of the code. Remember that all scripts are run in a single thread, in a linear fashion.

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 Ilhan · Jan 09, 2012 at 07:16 PM 0
Share

Thanks for your reply, that was the answer. Cheers! edit Sorry posted this as answer ins$$anonymous$$d of a comment again, some moderator delete it please.)

avatar image
0

Answer by Kryptos · Jan 09, 2012 at 11:53 PM

You could use a coroutine to exit the method every frame (after creating n blocks). This way Unity will have the possibility to check for inputs, collision and so on.

For example, let's say that you want to create 10 blocks each frame.

 function StartGrowing() {
     while (!stop) {
         var count = 0;
         var stop = false;
         while (count < 10) {
             AddSprite(block, Xpos, Ypos);
             Ypos += 16;
             ++count;
         } // end while
         yield; // wait for the next frame
     } // end while
 } // end function

 function VerticalHit() {
     stop = true;
 } // end function
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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

while(true) ??? 2 Answers

Problem with while statement. 1 Answer

How come my boost comes to an abrupt stop? 1 Answer

While loop crashing 2 Answers

Unity crashes when using while 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