What could be freezing unity?

Hi, in my game you are supposed to play as a vampire, and I am adding the mechanic to morph into a bat for short periods of time, however, something is causing unity to freeze.
The objective is simple in the script, there is one script that controls movement, and another script that controls the Player state (bat or vampire), but when I press the key to do it, unity freezes.

Movement Script:

using UnityEngine;
using System.Collections;

public class VampireControllerScript : MonoBehaviour {

	CharacterController controller;
	bool morphed = false;

	float speed;
	public float flySpeed = 8.0F;
	public float runSpeed = 6.0F;
	public float walkSpeed = 3.0F;
	public float jumpSpeed = 8.0F;
	public float gravity = 20.0F;
	private Vector3 moveDirection = Vector3.zero;

	bool walking;

	public CameraTargetScript cameraTargetScript;

	void Start () {
		controller = GetComponent<CharacterController>();
	}

	void Update() {
		if (!morphed){
			if (Input.GetButton("Sneak")) {
				if (controller.isGrounded) {
					walking = true;
				}
				else {
					walking = false;
				}
			}
			else {
				walking = false;
			}

			if (walking)
				speed =  walkSpeed;
			else
				speed = runSpeed;
			
		    if (controller.isGrounded) {
				moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
				moveDirection = transform.TransformDirection(moveDirection);
				moveDirection *= speed;

			if (Input.GetButton("Jump"))
				moveDirection.y = jumpSpeed;
				
			}

			moveDirection.y -= gravity * Time.deltaTime;
			controller.Move(moveDirection * Time.deltaTime);
		}
		else {
			moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
			moveDirection = transform.TransformDirection(moveDirection);
			moveDirection *= flySpeed;
			
			if (Input.GetButton("Jump"))
				moveDirection.y = jumpSpeed;
			
			
			controller.Move(moveDirection * Time.deltaTime);
		}
	}

	void LateUpdate () {
		if (!morphed)
			transform.rotation = Quaternion.Euler (0f, cameraTargetScript.currentYRotation, 0f);
		else
			transform.rotation = Quaternion.Euler (cameraTargetScript.currentXRotation, cameraTargetScript.currentYRotation, 0f);
	}

	void Morph () {
		if (morphed)
			morphed = false;
		else
			morphed = true;
	}
}

And the script to change state:

using UnityEngine;
using System.Collections;

public class VampireScript : MonoBehaviour {

	CharacterController controller;
	public VampireControllerScript controllerScript;

	public float coolDown = 15f;
	public float duration = 5f;
	float startCoolDown;
	float startDuration;

	public float defaultHeight = 2f;
	public float morphedHeight = 1f;
	public float defaultYposition = 1f;
	public float morphedYposition = 0.5f;

	bool morphed;

	void Start () {
		controller = GetComponent<CharacterController>();
		startCoolDown = coolDown;
		startDuration = duration;
		coolDown = 0f;
	}

	void Update () {
		if (Input.GetButtonDown("Special Ability") && morphed) {
			Morph ();
		}
		if (Input.GetButtonDown("Special Ability") && !morphed) {
			if (coolDown <= 0f) {
				Morph ();
			}
		}

		if (!morphed) {
			controller.height = defaultHeight;
			controller.center = new Vector3 (0f, defaultYposition, 0f);
			//default graphics
			coolDown -= Time.deltaTime;
			duration = startDuration;
		}
		else {
			controller.height = morphedHeight;
			controller.center = new Vector3 (0f, morphedYposition, 0f);
			//morphed graphics
			coolDown = startCoolDown;
			duration -= Time.deltaTime;
			if (duration <= 0f) {
				Morph();
			}
		}
	}

	void Morph () {
		if (!morphed)
			morphed  = true;
		else
			morphed = false;

		controllerScript.SendMessage("Morph", SendMessageOptions.RequireReceiver);
	}
}

So, do you know what could be freezing unity? Thanks in advance.

controllerScript.SendMessage() calls every method named “Morph” on every script attached to this GameObject. This includes itself, and as your calling method in VampireScript is allso called “Morph”, you’ll end up with an infinite loop. Rename one of your methods.

An infinite loop is freezing you. The vampire Morph function is calling itself (with the sendMessage) over and over and over… . I don’t use SendMessage and don’t know what you’re trying to accomplish, so can’t suggest how to fix it (is the vampire morph trying to tell the vampControlScript to morph?)

For a test, add global int morphCount=0; and at the start of vampire morph add morphCount++; Debug.Log("mc="+morphCount); if(morphCount>=50) return;. It should blast out 1-50 morphs on a single keypress.

It is a real pain that an infinite loop in code freezes all of Unity, so you can’t even click Stop. You just have to remember to SaveScene before running risky code.