Why does unity keep crashing when I use this script

Whenever I press Left Shift in play mode which is my running cue, Unity freezes and I need to use task manager to close it. Does anyone know what to do if this happens. Here is my script

#pragma strict
var Hands : Transform;
var PlayAnimation : boolean = false;

function Update()
{
while(Input.GetKeyDown(KeyCode.LeftShift))
{
PlayAnimation = true;
if(PlayAnimation == true)
{
Hands.animation.Play(“GlockRun”);
}
}

if(Input.GetKeyUp(KeyCode.LeftShift))
{
	PlayAnimation = false;
}

}

Well maybe it’s the crappy formatting, but your syntax seems to be a little wonky- and while loops tend to be really touchy in the first place, so I would recommend getting rid of it if you can.

Try this-

if(Input.GetKeyDown(KeyCode.LeftShift))//Get key down is only called on the frame where the key goes down- it's not continuous.
		{
			PlayAnimation = true; //Since you're using a bool, all you need is to change it once anyway!
		}

		if(Input.GetKeyUp(KeyCode.LeftShift)) // Same deal for get key up!
		{
			PlayAnimation = false;
		}

		if(PlayAnimation == true)
		{
			Hands.animation.Play("GlockRun");
		}

I haven’t tested that, but I don’t think it should crash on you anymore! The other possibility would be if you don’t have an animation component on Hands. in that case Unity would hang for a long time and then maybe throw a fit about how it cant find the animation component.

It won’t work because GetKeyDown is only true on the first update, then it is false. It is only triggered for one frame after which the held is true. There are 3 states, down, held and up. You should really not use PlayAnimation to do this but instead have the button held set to increase your speed by a run multiplier variable. Then in Animator window your transition between idle, walk and run animations should have parameters on them called speed and in the area to the right of the screen in inspector atomic conditions set speed for each animation to perhaps speed some walk value. That way, as soon as you press and release the keys( as long as you code for it) you will automatically transition from idle to walk to run to walk to idle

Sorry, didn’t see this was answered already. The 2d controllers tutorial on here shows a really easy way to transition.