Unity Hangs when testing the game

Everytime I press the play button, unity hangs and is not responding via the taskmanager.

I’ve tried closing out each code and have found that it only occurs when
this code is included.
This is the code for my 3rd person camera script. Though I’m not sure which part of the code causes the hang. Can anyone please help me check for the problem?

using UnityEngine;
using System.Collections;

public class TP_Camera : MonoBehaviour {
	public static TP_Camera Instance;
	public Transform TargetLookAt;
	public float Distance = 5f;
	public float DistanceMin = 3f;
	public float DistanceMax = 10f;
	public float X_MouseSensitivity = 5f;
	public float Y_MouseSensitivity = 5f;
	public float MouseWheelSensitivity = 5f;
	public float Y_MinLimit = -40f;
	public float Y_MaxLimit = 80f;
	public float DistanceSmooth = 0.05f;
	public float X_Smooth = 0.05f;
	public float Y_Smooth = 0.1f;
	private float velX = 0f;
	private float velY = 0f;
	private float velZ = 0f;
	private Vector3 position = Vector3.zero;
	private Vector3 desiredPosition = Vector3.zero;
	private float velDistance = 0f;
	private float mouseX = 0f;
	private float mouseY = 0f;
	private float startDistance = 0f;
	private float desiredDistance = 0f;
	void Awake ()  {
		Instance = this;	
	void Start () {
		Distance = Mathf.Clamp(Distance,DistanceMin,DistanceMax);
		startDistance = Distance;
	void LateUpdate () {
		if(TargetLookAt == null)
	void HandlePlayerInput(){
		var deadZone = 0.1f;
			//RMB is down thus true, get mouse axis input
			mouseX += Input.GetAxis("Mouse X") * X_MouseSensitivity;
			mouseX -= Input.GetAxis("Mouse Y") * Y_MouseSensitivity;
		// This is where we will limit mouseY
		mouseY = Helper.ClampAngle(mouseY,Y_MinLimit,Y_MaxLimit);
		if (Input.GetAxis("Mouse ScrollWheel") < -deadZone || Input.GetAxis("Mouse ScrollWheel") > deadZone){
			desiredDistance = Mathf.Clamp(Distance - Input.GetAxis("Mouse ScrollWheel") * MouseWheelSensitivity
	void CalculateDesiredPosition(){
		//Evaluate the distance
		Distance = Mathf.SmoothDamp(Distance, desiredDistance,ref velDistance, DistanceSmooth);
		//Get Desired Position
		desiredPosition = CalculatePosition(mouseY, mouseX, Distance);
	Vector3 CalculatePosition(float rotationX, float rotationY, float distance){
		Vector3 direction = new Vector3(0,0,-distance);
		Quaternion rotation = Quaternion.Euler(rotationX,rotationY,0);
		return TargetLookAt.position + rotation * direction;
	void UpdatePosition(){
		var posX = Mathf.SmoothDamp(position.x,desiredPosition.x,ref velX, X_Smooth);
		var posY = Mathf.SmoothDamp(position.x,desiredPosition.y,ref velY, Y_Smooth);
		var posZ = Mathf.SmoothDamp(position.z,desiredPosition.z,ref velZ, X_Smooth);
		position = new Vector3(posX,posY,posZ);
		transform.position = position;
	public void Reset(){
		mouseX = 0;
		mouseY = 10;
		Distance = startDistance;
		desiredDistance = Distance;
	public static void UseExistingOrCreateNewMainCamera()
		GameObject tempCamera;
		GameObject targetLookAt;
		TP_Camera myCamera;
		if(Camera.mainCamera != null){
			tempCamera = Camera.mainCamera.gameObject;	
			tempCamera = new GameObject("Main Camera");
			tempCamera.tag = "MainCamera";
		myCamera = tempCamera.GetComponent("TP_Camera") as TP_Camera;

		targetLookAt = GameObject.Find("targetLookAt") as GameObject;
		if (targetLookAt == null){
			targetLookAt = new GameObject("targetLookAt");
			targetLookAt.transform.position = Vector3.zero;
		myCamera.TargetLookAt = targetLookAt.transform;

The function UseExistingOrCreateNewMainCamera attaches a new TP_Camera-Component each time you call the function. This is a dangerous singleton implementation. Beside that i don’t see a problem with that code. It’s just a bit strange that you use the string version of AddComponent / GetComponent. The generic version is typesafe and much easier to use:

myCamera = tempCamera.GetComponent<TP_Camera>();
if (myCamera == null)
    myCamera = tempCamera.AddComponent<TP_Camera>();