Unity Stops Working Whenever I Try To Test A Specific Code

I’m very new to unity and coding, and as I was messing around trying to learn how to code in C#, I used a while loop in one of my scripts. Once I tried to test out the script unity became unresponsive and I couldn’t even close unity. I had to restart my computer and once I tried again it did the same thing again. I don’t know if this is a problem with my computer, unity, or my code but if anyone could help me out that would be great.

Without actually seeing the code in question, it’s going to be mighty difficulty for us to help you.

And if you are posting code, please use code tags.

1 Like

You’ve created an infinite loop, in that case.
Unity’s execution will not proceed until the loop finishes, but since the loop is infinite, Unity will just be waiting forever.

Here are some examples of infinite loops:

while(true) {
  //We're explicitly telling this loop to run forever, since the condition is just "true".
}
int loopCount = 10;
int currentLoop = 1;

while(currentLoop <= loopCount) {
  Debug.Log("Loop number: " + currentLoop);

  //We forgot to increase currentLoop here. The condition will never be false, creating an accidental infinite loop.
}

You need to re-evaluate your loop condition to ensure it’s possible for it to become false.