unity crashes when preforming a 'while' + 'if' loop

hi all,
im trying to create an interact button. i’ve created the script for box coliider triggers (basically the console print out when the player collider touched something else) and i want to make it so it would print it, only
while colliding with something else + pressing ‘e’. for this i’ve created the following code:

 while (triggering)
        {
            if (Input.GetKey("e"))
            {
                print("player is interacting with " + triggeringNpc);

            }
        }

this code crashes unity. if i go inside another box collider the engine registers it, only instead of waiting for me to press e it freezes up and crashes.

any ideas?

BTW** i have fixed it by changing the while to an if statement, but i’ll still be happy to know why it doesn’t work

Unity is single-threaded from a scripting standpoint. When you enter a while loop, the engine will never run again so your input won’t happen.

Instead use a coroutine, and within the while loop be sure that you yield.

Infinite loop.

A while loop literally executes the code it contains while the condition is true. That doesn’t mean over multiple frames, it means instantly, and forever. Your while loop never sets triggering to false, so there’s no way for the execution to leave, and it goes on forever and causes a crash.

I’m assuming that that while loop is in Update, which runs once a frame. By changing the while loop to an if, instead of checking the e key instantly and forever, you’re checking it once a frame (but only if triggering is true), which is what you want.

1 Like

i’m sorry but didn’t understand anything you said. i’ve forgot to mention that i am very new to game deving.
what is a coroutine, why does it mean to yield and does that mean i won’t be able to use any while loops in unity anymore?
thanks

You can use while loops, you just need to use a condition that will be false at some point. If your condition never changes to false, the program will never exit the loop, and unity will never be able to finish processing the current frame because your code is still running. Your code needs to finish at some point so Unity can continue the game loop.

yup exactly what i did. i’ve updated the thread question with the fact that i’ve solved it. thanks for explaining why tho.
i still don’t fully understand why it makes it crash. i mean a while loop happens only while a certain pramater is true, but that also means it would be happening if the player would just leave the box collider’s range.

That can never happen because nothing can move until your while loop exits. The entire game is waiting for your code to finish running before continuing. That’s what @Kurt-Dekker means by single-threaded.

When thinking about code execution flow, imagine that your Update method is a route on your GPS. The opening bracket is your point A, and you’re trying to get to point B, the closing bracket. Imagine that other tasks that the game engine executes are different GPS routes. There’s only one car completing all these routes, one after the other, and when it’s in your Update statement you have complete control of that one car.

If statements and while loops are like modifications to that route based on some criteria. For example, your if( triggering ) could be interpreted as taking an alternate route from point A to point B, if triggering is true (like if the route changes based on traffic).

If you substitute if for while, it’s like putting a slot in your route that just says “turn right while triggering is true”. There’s nothing inside that while loop that makes triggering false, so you will just make right turns forever, going in a pointless circle. Your car never gets to point B and finish the route, and therefore you can never hand the car over to be driven by someone else, and so nothing happens except turning right forever, and your game crashes.

1 Like

go it. thanks!