- Home /
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.
Answer by robertbu · Nov 23, 2014 at 05:24 PM
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.
Dear robertbu,
First of all, thank you for your response.
Second of all, the reason why I used this.transform.position ins$$anonymous$$d of any other method was due to me thinking that the action of falling/rising was the reason why the program crashed, so I used that ins$$anonymous$$d of what I originally had, which was ridigbody.addforce. Does that make my code any better? Of course not, there are still plenty of issues with it that I must admit, I am a bit ashamed of missing before (This is why you have multiple people look at programs.)
Anyway, I will look into the tutorials, and come back with a better form of this code now that I understand where things broke. Thank you for your help.
Sincerely,
TheDe$$anonymous$$
You might want to get rid of all the unnecessary ";" as well, you don't need to put one after the closing "}" of if statements, functions, and while loops. :)
Here is the updated, functioning code, for anyone that is interested. Adjust the variables at your desire.
#pragma strict
public var jumpSpeed = 5;// Speed at which the character rises/falls
public var isHovering = false;// Boolean that checks if the character is in a 'hovering' state
public var gravity = 2;
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 ()
{
Debug.Log(isHovering);
if (isHovering)
{
rigidbody.AddForce(Vector3(0, jumpSpeed, 0));
isHovering = false;
}
rigidbody.AddForce(Vector3(0, -gravity * Time.deltaTime, 0));
};//end Update
@TheDe$$anonymous$$ - I suggest you move lines 20 - 24 into FixedUpdate(). Calling AddForce() in Update() will produce inconsistent results depending on the frame rate.
Your answer
Follow this Question
Related Questions
Script crashes Unity 1 Answer
A node in a childnode? 1 Answer
Unity webplayer crashing browser when it is loaded and unloaded into a page using JavaScript 0 Answers
Instantiate OnDestroy 1 Answer
Making changes to a terrian in game 1 Answer