My code involves me waiting for user input. The code cannot progress unless the trigger is clicked on the vive controller. However, when I try to incorporate a while loop, unity crashes.
void ThrowBall()
{
trackedController = GameObject.FindWithTag("Right Controller").GetComponent<SteamVR_TrackedController>();
ballscript = GameObject.FindObjectOfType(typeof(BallScript)) as BallScript;
if (ballscript.getLocation() == 2)
{
self.StopPlayback();
player1.StopPlayback();
player2.StopPlayback();
self.Play("idling");
player1.Play("idling");
player2.Play("idling");
while (trigger == false)
{
int x = 0;
x = x + 1;
Debug.Log(x);
}
}
ballscript.ReleaseMe();
}
Unity apps are single threaded programs unless you make new threads yourself. Each thread executes the code line by line by line (simplification). Since you got only one thread, the execution of code can only be happening at a singular point at any given time. So when you have a loop like this
while (trigger == false)
{
int x = 0;
x = x + 1;
Debug.Log(x);
}
those are the only 3 lines of code that are executing in the whole app. That’s including the code in Unity’s engine where animations are played, screen is redrawn, sounds are played, and most importantly in your case, input is gathered. Unity can’t even display the Debug.Logs since it’s stuck in the loop (since you reintroduce int i every time in the loop, it would print out 1 every time anyways)
Nowhere inside that loop you give trigger
a chance to change its value so the loop could ever end.
You don’t say what it is you actually want to do in that loop/instead of the loop, but whatever it is, you have to enable changing trigger back to true inside the loop. Or setting a proper start condition, doing your stuff in Update while that lasts, and making another condition for stopping doing it.
If what you want to do requires the screen to be redrawn after each change (animating a position or such), you’ll have to do it the latter way (no loop, just do stuff each Update as a condition is met) because a tight loop won’t allow it.
would i be able to check for a trigger input in update? would that work?