While Loop consistently crashing Unity

Heya, I’m working on a turn-based strategy game where a piece has a set movement speed in squares(able to move north, south, east, and west in 1 square) which reduces the remaining distance(remdist) by 1. This is all in a function, “onMove” which is supposed to continue until remdist is 0(clicking end movement will also reduce remdist to 0). For some reason, Unity crashes whenever I run this script, however, and I am unsure why. What is causing this?

Code box is not working for me for unknown reasons, so I’m going to add br tags and pray it works this time:
@script ExecuteInEditMode()

var speed : int = 5;

var attack : int = 5;

var def : int = 3;

var hp : int = 10;

var acc : float = .9;

var eva : float = .1;

var unitCamera : Transform;

function OnGUI () {

unitCamera.position.x = transform.position.x;

unitCamera.position.y = transform.position.y + 4.5;

unitCamera.position.z = transform.position.z;

if (GUI.Button(Rect((Screen.width) - 145,(2 * Screen.height/3) + 50,135,25),"Attack")) {

print ("Chose to Attack!");

}

if (GUI.Button(Rect((Screen.width) - 145,(2 * Screen.height/3) + 85,135,25),"Move")) {

print ("Chose to Move!");

onMove(speed);

}

if (GUI.Button(Rect((Screen.width) - 145,(2 * Screen.height/3) + 120,135,25),"End Turn")) {

print ("Turn Ended!");

}

}

function onMove (remdist) {

GUI.Box(Rect((Screen.width) - 145,(2 * Screen.height/3) - 50,135,25),"Distance Left: " + remdist + "sq");

if (remdist>0){

	if (GUI.Button (Rect ((Screen.width) - 145,(2 * Screen.height/3) - 20,65,65), "West")) {

		print ("You clicked West!");

		transform.Translate (-1, 0, 0);

		remdist -= 1;

		unitCamera.position.x = transform.position.x;

		unitCamera.position.y = transform.position.y + 4.5;

		unitCamera.position.z = transform.position.z;

	}

	if (GUI.Button (Rect ((Screen.width) - 145,(2 * Screen.height/3) + 50,65,65), "South")) {

		print ("You clicked South!");

		transform.Translate (0, 0, -1);

		remdist -= 1;

		unitCamera.position.x = transform.position.x;

		unitCamera.position.y = transform.position.y + 4.5;

		unitCamera.position.z = transform.position.z;

	}

	if (GUI.Button (Rect ((Screen.width) - 75,(2 * Screen.height/3) - 20,65,65), "North")) {

		print ("You clicked North!");

		transform.Translate (0, 0, 1);

		remdist -= 1;

		unitCamera.position.x = transform.position.x;

		unitCamera.position.y = transform.position.y + 4.5;

		unitCamera.position.z = transform.position.z;

	}

	if (GUI.Button (Rect ((Screen.width) - 75,(2 * Screen.height/3) + 50,65,65), "East")) {

		print ("You clicked East!");

		transform.Translate (1, 0, 0);

		remdist -= 1;

		unitCamera.position.x = transform.position.x;

		unitCamera.position.y = transform.position.y + 4.5;

		unitCamera.position.z = transform.position.z;

	}

}

if (GUI.Button(Rect((Screen.width) - 145,(2 * Screen.height/3) + 120,135,25),"End Movement")) {

print ("Movement Ended!");

remdist = 0;

}

}

  • What you have here is an infinite loop.
    Think about what happens when none of your “if” conditions are met.
  • a while loop is not the right solution anyway, use the Update() fonction for this kind of things