Hey everyone! I just noticed something I wanted to point out: I tend to have bad experiences with while loops in Javascripts. When I run the game, they usually cause Unity to crash. Does this happen to anyone else? What causes that?
they shouldn’t crash and haven’t had it happen so far
but you might want to avoid declare variables in the loop declaration.
so no
while( var blabla in blablubb)
I expect you’re doing something like
while (something) {
// do stuff
}
where “something” is always true, which makes an infinite loop, which freezes Unity. So don’t do that. Make sure your conditions cause the loop to exit, and put a yield in there if you need to, at least for debugging. Definitely nothing wrong with while loops if used correctly.
–Eric
awesome, thanks! I figured it was something like that and you’re right, it was an infinite loop
If you are still unsure why something is freezing Unity, Debug.Log is useful. The system console will show an unending stream of entries if you are running an infinite loop.
… well, at least given that you know where the infinite loop is and can put Debug.Log into it. The tricky thing with these infinite loops is that frequently, you don’t know where it is (and it’s pretty difficult to find where they are, especially if it’s not a trivial case).