Program Crashes Unity

I am writing code that makes the player rise up into the air, and then ‘glide’ (still maintaining basic movement, while slowly falling back down) after they hit an item. The problem is, it crashes Unity every time I run it. As in, I have to forcequit out of it every time I try testing it.

#pragma strict

public var Speed = 10;// Speed at which the character rises/falls
public var isHovering = false;// Boolean that checks if the character is in a 'hovering' state
public var beginningHoveringTime = 5;//Time (in seconds) it takes for a character to rise to hover state
public var endingHoveringTime = 5;//Time (in seconds) it takes for a character to lower back to the ground
public var totalHoverTime = 30;//Total time the player hovers in the air

// Use this for initialization
function Start () {
	
};

function OnCollisionEnter(target : Collision){
	
	if (target.gameObject.tag =="Hover")
	{
		isHovering = true;
		Destroy(target.gameObject);
	}//end if
};//end OnCollisionEnter


// Update is called once per frame
function Update () 
{
	while(isHovering && beginningHoveringTime > 0) //checks if the bool has been triggered,										//and the beginningHoverTimer is not finished	
	{			
		beginningHoveringTime -= Time.deltaTime;
		
		if (beginningHoveringTime > 0)//start of hovering
				this.transform.position = Vector3(0, 5, 0);//Makes the character rise

	};//end while
	
	totalHoverTime -=Time.deltaTime;//Timer starts ticking while the player hovers around
	
	rigidbody.velocity.y = -1;
	
	if (rigidbody.velocity.y < -5)
		rigidbody.velocity.y = -1;
		
	if (totalHoverTime <= 0 && isHovering)//Timer has completed, and the player will fall
	{
		isHovering = false;
		rigidbody.velocity.y = 0;
		
		endingHoveringTime -= Time.deltaTime;//makes sure the character falls at the same rate each time
		
		while (endingHoveringTime > 0)
			this.transform.position = Vector3(0, -5, 0);////Makes the character fall
	};//end if
	beginningHoveringTime = 5;//These two lines reset the timers for multiple uses
	endingHoveringTime = 5;
};//end Update

This code is in Javascript, with a c# script that adds torque to the ball (to move it around). Is it the torque script being C# and this script being in Javascript the reason that is causing it to crash, and if so, is there any way to convert my script into C#, or vice versa? Thank you in advance for your help.

Your problem is on line 50 - 52:

 while (endingHoveringTime > 0)
         this.transform.position = Vector3(0, -5, 0);////Makes the character fall
 };

Once you enter this while loop, nothing changes ‘endingHoverTime’ and therefore your app hangs forever executing this while loop.

There are a number of issues here. First, when writing code like this, you want to do a bit of work every Update() call. Both the while loop on line 50 and the one on 27 are not the way you want to handle movement. Update() gets called once a frame, so you want to figure out how to do the movement for that frame.

Also there are other issues. For example:

  this.transform.position = Vector3(0, -5, 0);////Makes the character fall

…sets the absolute position of your game object. It does not make it fall. To make it fall consider using Transform.Translate(). And you want to explore the use of Time.deltaTime in your movements.

I recommend you take a step back and do some basic tutorial on movement and write some simple test scenes. After you have mastered the concepts of movement, come back and rewrite this code.