Unity crashes while running a certain function.

I am trying to set up a function that adds a jump boost to the main character when a jump potion is used, but unity crashes whenever I use the potion. This is the code inside of the JumpBoost function.

	public void JumpBoost()
	{
		int boostTime = 15;
		float boosted = 0f;
		boosted = Time.time + boostTime;
		while(Time.time < boosted)
		{
			jumpForce = 3f;
		}
		jumpForce = 2f;
	}

And this is how I am calling the function:

case 3:
{
player.GetComponent().JumpBoost();
break;
}
}

I am getting no errors when compiling and the game will run fine so I assume its something with my while loop, but it shouldn’t be an infinite loop. I’ve tryed adding a delay inside the while loop also, but that didn’t help at all. Thanks in advance for anyone who can help me :slight_smile:

Time.time is the time at the beginning of this frame. Your while never leaves so you remain inside that frame and time is never increased.

Using a coroutine would fix the issue.

 public IEnumerator JumpBoost()
 {
     int boostTime = 15;
     float boosted = 0f;
     boosted = Time.time + boostTime;
     jumpForce = 3f;
     while(Time.time < boosted)
     {
         yield return null;
     }
     jumpForce = 2f;
 }