Unity freezes when script is run in play mode
I am trying to make a landing script for a plane and whenever the script runs it freezes Unity and I have to close it with Task Manager. Whenever I reopen unity the script is disabled due to it not saving when I activated it , and when I hit play without it being activated, it works fine, so it only freezes when the script is activated. Here is the script: Oh, and I use Unity 2018.1, If that is a factor in why it freezes.
public class TieControl : MonoBehaviour {
AudioSource audioSource;
// Use this for initialization
void Start () {
// the is landed by default
land = true;
}
// Update is called once per frame
// when the landing variable is 1, that means it is taking off, when 2, that means it is landing, when 0, that means it has landed or is in the air.
// when land is false, that means it is in the air, when true, that means it is on the ground landed.
public bool land = true;
public int landing = 0;
void Update() {
//default movement without input
if (land == false)
transform.Translate(0f, 0f, -0.2f);
if (Input.GetKey(KeyCode.Space) & (land == false))
landing = 2;
if (Input.GetKey(KeyCode.Space) & (land == true))
landing = 1;
//everything under here is only if land is false
if (Input.GetKey(KeyCode.W) & (land == false))
transform.Translate(0f, 0f, -0.5F);
if (Input.GetKey(KeyCode.S) & (land == false))
transform.Translate(0f, 0f, 0.5f);
if (Input.GetKey(KeyCode.A) & (Input.GetKey(KeyCode.S) & (land == false)))
transform.Rotate(0f, -0.5f, 0f);
else if (Input.GetKey(KeyCode.A) & (land == false))
transform.Rotate(0f, -1, 0f);
if (Input.GetKey(KeyCode.D) & (Input.GetKey(KeyCode.S) & (land == false)))
transform.Rotate(0f, 0.5f, 0f);
else if (Input.GetKey(KeyCode.D) & (land == false))
transform.Rotate(0f, 0.5F, 0f);
if (Input.GetKey(KeyCode.UpArrow) & (Input.GetKey(KeyCode.S) & (land == false)))
transform.Rotate(0.5f, 0f, 0f);
else if (Input.GetKey(KeyCode.UpArrow) & (land == false))
transform.Rotate(1f, 0f, 0f);
if (Input.GetKey(KeyCode.DownArrow) & (Input.GetKey(KeyCode.S) & (land == false)))
transform.Rotate(-0.5f, 0f, 0f);
else if (Input.GetKey(KeyCode.DownArrow) & (land == false))
transform.Rotate(-1f, 0f, 0f);
if (Input.GetKey(KeyCode.LeftArrow) & (Input.GetKey(KeyCode.S) & (land == false)))
transform.Rotate(0f, 0f, -0.5f);
else if (Input.GetKey(KeyCode.LeftArrow) & (land == false))
transform.Rotate(0f, 0f, -1f);
if (Input.GetKey(KeyCode.RightArrow) & (Input.GetKey(KeyCode.S) & (land == false)))
transform.Rotate(0f, 0f, 0.5f);
else if (Input.GetKey(KeyCode.RightArrow) & (land == false))
transform.Rotate(0f, 0f, 1f);
//everything under here is if Landing is true to allow turning while landing
if (Input.GetKey(KeyCode.A) & (landing == 2))
transform.Rotate(0f, -0.2f, 0f);
if (Input.GetKey(KeyCode.D) & (landing == 2))
transform.Rotate(0f, 0.2f, 0f);
//everything under here is the landing movement and the takeoff movement\
//landing
while (landing == 2)
{
transform.Translate(0f, -.2f, 0f);
if ((landing == 2) & (transform.position.y == 40))
landing = 0;
}
//takeoff
while (landing == 1)
{
transform.Translate(0f, .2f, 0f);
land = false;
landing = 0;
}
}
//everything under here has to do with the collider
// this will change landing to 0 when it collides with the terrain
void OnTriggerEnter(Collider other)
{
print("The ship has landed");
land = true;
landing = 0;
{ };
}
}
Answer by highpockets · Dec 08, 2020 at 07:13 PM
Your while loops are blocking/stopping the thread and landing will never change to 1 or 2 while it’s in there because the Update method will not be run again. You have to rethink your logic. Either take those while loops out of Update and put them in coroutines with yield statements or work out a way to include the while loop logic within your Update function without blocking the main thread
Your answer
Follow this Question
Related Questions
Unity Crashes When Clicking Play: Script Error? 1 Answer
Unity freezes. I dont know why... 3 Answers
Unity freeze on certain condition. 0 Answers