Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 xToxicInferno · Mar 02, 2010 at 05:23 PM · freezewhile-loop

Unity Crashes From While Command

This is the second 'While' Command that has crashed Unity, and i have no idea why. I have them both for you to look at, but here is the latest culprit:

static var eleswitch : int; var trigger : int = 1; var Player : Transform; var EleDoor1 : Transform; var EleDoor2 : Transform; var EleDoor1Open : Vector3; var EleDoor2Open : Vector3;

var openSpeed : int = 5;

function OnTriggerStay (Other : Collider) { eleswitch = trigger; Player.parent = null; DoorOpen(Other); }

function DoorOpen (other : Collider) { var door1close = EleDoor1.transform.position.z; var door2close = EleDoor2.transform.position.z; while (door1close < EleDoor1Open.z) { EleDoor1.transform.Translate(-Vector3.right*Time.deltaTime*openSpeed); } while (door2close < EleDoor2Open.z) { EleDoor2.transform.Translate(Vector3.right*Time.deltaTime*openSpeed); } }

Just ask, and i could post the other one for you to look at and see if it is the same thing or not. This is annoying as the 'while' command is very useful, and i like to test each part of my script as i write it, as such it usually crashes before i have saved...a bad habit of mine i guess, but i was wondering if there was a fix to this or not.

Comment
Add comment · Show 2
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 AngryAnt ♦♦ · Mar 03, 2010 at 08:00 AM 0
Share

Rather than just asking people to "help me fix this!", maybe you could provide some additional information?

avatar image Motionreactor · Mar 03, 2010 at 12:32 PM 0
Share

Do you actually understand how a while loop works? Doesn't look like it to me. You need to go back to basics and read about how while loops actually work, because you are using them in a way which is guaranteed to cause problems.

While loops not only repeat until the condition is met, but they also halt the interpreter from moving onto the next piece of code. The interpreter simply won't call the next statement until you break out of the while loop. So your second while loop doesn't even get executed at all, because it will never get past the first while. loop.

7 Replies

· Add your reply
  • Sort: 
avatar image
4

Answer by duck · Mar 02, 2010 at 05:37 PM

If a "While" loop causes a crash or causes Unity to lock up and hang, it's because the loop is never reaching its end condition. It looks to me like you're causing an infinite loop. The reason being that you're checking:

(door2close < EleDoor2Open.z)

....to see whether the loop should finish, but you're nor modifying either of those two values inside the loop, so it will always be true and never finish executing.

This may come from a mistaken assumption that the earlier line:

var door2close = EleDoor2.transform.position.z;

....creates a link, or a "reference". It does not - it in fact just copies the value into "door2close". So if you subsequently modify "EleDoor2.transform.position.z", those changes won't be reflected in the "door2close" variable.

Basically to solve the "crashing" problem, you need to ensure that the condition specified in the "While" loop can be met, during the execution of the contents of that loop.

However...

There appears to be numerous other problems with your code which indicate that the general approach you are taking to achieve your goal might be flawed.

So, while I hope this particular answer may help you to understand why a "While" loop can cause Unity to freeze up (and therefore on a broad level, answers this specific question) it won't necessarily help you all that much towards completing your script as a whole.

Might I suggest starting a new question, and ask a broader question aimed at finding the best technique to achieve your particular goal? Such as:

"How can I make a door open and close when a collider is touched?"

(if that happens to be your goal)
You could then optionally post the code that you have so far, and it would be more relevant for people to post alternative suggestions to how it could work, without having to guess at the bigger picture.

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 xToxicInferno · Mar 02, 2010 at 06:11 PM 0
Share

Changing it to 'while (door2close < EleDoor2.transform.position.z)' does not work, as it will never run becasue they start out at both being the same number, thus door2close will never be less than it. Also if i were to use

avatar image duck ♦♦ · Mar 03, 2010 at 01:19 PM 0
Share

Please see the additional text I have added to my answer, under the title "However..." :-)

avatar image
1

Answer by lowbloodsugar · Mar 03, 2010 at 04:19 AM

Everything that the previous person said is on right track, but you also need a yield statement!

Put this

yield;

at the end of each loop. And you will notice that your program doesn't crash. You will still need to make sure you have your conditional logic correct.

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 Lipis · Mar 03, 2010 at 04:41 AM 0
Share

The "previous person" is not really relevant here.. because the order of the answers might change due to order by rates.This would be better as a comment so @Duck could also change in his answer. Regards

avatar image lowbloodsugar · Mar 05, 2010 at 11:38 AM 0
Share

And look at that! The @Duck co-routine changed his answer while the @clement co-routine was yielding on the couch!

avatar image
1

Answer by Waz · Jun 08, 2011 at 04:42 AM

Looks like you just needs to use an if instead of a while (since you are calling it every frame anyway), but still all the above concerns apply.

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
avatar image
0

Answer by Ashkan_gc · Mar 03, 2010 at 07:17 AM

duck is right but i think there is another problem. OnTriggerStay might be executed several times and it will execute your loop several times too. it's a really infinite or a big loop that might take several seconds so the unity will crash. why don't you use OnTriggerEnter or OnTriggerExit?

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
avatar image
0

Answer by Motionreactor · Mar 03, 2010 at 03:44 PM

Please refer to:

http://answers.unity3d.com/questions/5045/using-a-gui-button-to-stop-a-gameobject-movement

Your other question for a related solution.

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
  • 1
  • 2
  • ›

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

2 People are following this question.

avatar image avatar image

Related Questions

How to use a WHILE LOOP? Obviously not inside ***function Update*** 3 Answers

While loop causes game to freeze 2 Answers

beginner coder question, is this code right? 1 Answer

Unity Crashes From While Loop and Can't fix 0 Answers

Do-while loop freezes the engine when running the game -1 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