CppPythonDude
CppPythonDude

Reputation: 69

Unity program stopped responding because of a infinite while loop

Hello stack overflow users! I was creating a game in unity 2018.2.2f1. All went great, but i missplaced the order of code lines in the program --> which caused a infite while loop appearing. I didn't know it would be infinite so I runned the program. Now my unity window is unselectable when i click on the icon, and when i enter the window, everything is frozen. I would close the program trough task manager, but i didnt save my files and i can't even save the project. Is there any way for me to save the project when it is in this state? My unity didn't crash, it just froze. Here is my FIXED c# code if that somehow helps (and i am in visual studio):

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Movement : MonoBehaviour {

    public float speed = 10f;
    private Vector3 velocity = new Vector3();
    private string direction = "none";
    private bool canMoveAgain = true;
    private bool go = false;
    // Use this for initialization
    void Start () {
        speed = 10f;
    }

    // Update is called once per frame
    private void OnCollisionEnter2D(Collision2D collision)
    {
        if (collision.collider.name == "wall (3)" && direction == "down")
        {
            go = false;
        }
        if (collision.collider.name == "wall (2)" && direction == "up")
        {
            go = false;
        }
        if (collision.collider.name == "wall" && direction == "left")
        {
            go = false;
        }
        if (collision.collider.name == "wall (1)" && direction == "right")
        {
            go = false;
        }
    }
    void Update () {
        if (Input.GetKeyDown(KeyCode.DownArrow) && canMoveAgain)
        {
            direction = "down";
            go = true;
            canMoveAgain = false;
            while (go)
            {
                velocity = new Vector3(0,-1,0);
                transform.position += velocity;
            }
            velocity = new Vector3(0, 0, 0);
            direction = "none";
        } else if (Input.GetKeyDown(KeyCode.UpArrow) && canMoveAgain)
        {
            direction = "up";
            go = true;
            canMoveAgain = false;
            while (go)
            {
                velocity = new Vector3(0, +1, 0);
                transform.position += velocity;
            }
            velocity = new Vector3(0, 0, 0);
            direction = "none";
        } else if (Input.GetKeyDown(KeyCode.LeftArrow) && canMoveAgain)
        {
            direction = "left";
            go = true;
            canMoveAgain = false;
            while (go)
            {
                velocity = new Vector3(-1, 0, 0);
                transform.position += velocity;
            }
            velocity = new Vector3(0, 0, 0);
            direction = "none";
        } else if (Input.GetKeyDown(KeyCode.RightArrow) && canMoveAgain)
        {
            direction = "right";
            go = true;
            canMoveAgain = false;
            while (go)
            {
                velocity = new Vector3(+1, 0, 0);
                transform.position += velocity;
            }
            velocity = new Vector3(0, 0, 0);
            direction = "none";
        }
    }
}

Upvotes: 0

Views: 382

Answers (2)

tomer zeitune
tomer zeitune

Reputation: 1202

First of all. Do not close Unity. You got two things you can try to do.

  • Luckily your code has a backup when you press run. It is inside the Temp folder. The Temp folder will be replaced if you press run again. Copy the project to somewhere safe.

  • Attach a Debugger, this can be either Visual Studio or MonoDevelop. Once they are attached to Unity just click pause. And you're free to save your project.

I hope you find this answer before it's too late.

Upvotes: 1

derHugo
derHugo

Reputation: 90813

You do

while (go)
{
    velocity = new Vector3(+1, 0, 0);
    transform.position += velocity;
}

but go is never set to false inside the loop.

OnCollisionEnter is never executed since the while is still being executed => freeze.

since Update is called each frame anyway you should rather move that part to a separate if(go) block after checking the Inputs ..

private void OnCollisionEnter2D(Collision2D collision)
{
    if (collision.collider.name == "wall (3)" && direction == "down"
        || collision.collider.name == "wall (2)" && direction == "up"
        || collision.collider.name == "wall" && direction == "left"
        || collision.collider.name == "wall (1)" && direction == "right")
    {
        go = false;
        velocity = Vector3.zero;
        direction = "none"
    }
}

void Update () 
{
    if (Input.GetKeyDown(KeyCode.DownArrow) && canMoveAgain)
    {
        direction = "down";
        go = true;
        canMoveAgain = false;
        velocity = -Vector3.up;
    } 
    else if (Input.GetKeyDown(KeyCode.UpArrow) && canMoveAgain)
    {
        direction = "up";
        go = true;
        canMoveAgain = false;
        velocity = Vector3.up;
    } 
    else if (Input.GetKeyDown(KeyCode.LeftArrow) && canMoveAgain)
    {
        direction = "left";
        go = true;
        canMoveAgain = false;

        velocity = -Vector3.right;
    } 
    else if (Input.GetKeyDown(KeyCode.RightArrow) && canMoveAgain)
    {
        direction = "right";
        go = true;
        canMoveAgain = false;
        velocity = Vector3.right;
    }

    if(go)
    {
        transform.position += velocity;
    }    
}

Note that currently your code is also frame rate dependent. You might want to rather use something like

public float moveSpeed;

...

transform.position += velocity * moveSpeed * Time.deltaTime;

For the direction I would rather recommend an enum like

private enum MoveDirection
{
    None,
    Up,
    Down,
    Left,
    Right
}

and use it e.g. like

direction = MoveDirection.Up;

and

if(direction == MoveDirection.Up)

which is more efficient than comparing strings.


Note: Typed on smartphone so no warranty but I hope the idea gets clear

Upvotes: 2

Related Questions