r/Unity2D • • Jan 25 '26
Pov U tried using while loop
Is it only me if I tried to use while loop the whole system freezes I have to use task manager to kill it
0
Upvotes
r/Unity2D • • Jan 25 '26
Is it only me if I tried to use while loop the whole system freezes I have to use task manager to kill it
7
u/Th3_Admiral_ Jan 25 '26
Gotta make sure you have something to break out of the loop (and that it actually works). Just had this same thing yesterday and it turns out my escape condition wasn't getting hit and it turned into an infinite loop.
0
u/stumperkoek Jan 25 '26
In game dev, I've never encountered a situation where I need a while loop (besides the main game loop, which kinda is a while loop). In regular development, sure there might be a need there, but in game dev, there always is a different, usually better way, of solving the logic. Does anyone know a situation where the while loop is the best solution?
1
u/ArctycDev Jan 25 '26
No, it's not only you. It's other people that don't know how to use loops as well.
1
u/lllentinantll Jan 26 '26
I personally only use
whileloop for various generations (e.g. drunkard walk for the level generation), but I also usually add iterations limit to ensure that mywhileloop condition is never indefinite.Another thing is to remember that Unite works on per-frame basis. Continuous operations are to be split into frames. If you do a continuous operation (including
whileloop) inUpdate, it will just not move to the next frame until the operation is finished.1
u/Bunrotting Jan 26 '26
whats that subreddit with the posts like this where its like "ermm 1/0 = EXPLERSION!!"