C# Drag rotation code crashing unity with no errors.

Hey,

I’m trying to implement the drag rotation script from this thread: http://forum.unity3d.com/threads/13440-Physics-Input-Questions in C# rather than java, but every time I try to play it unity crashes as soon as I click on my sphere. There are no compiling errors, no error messages, it just freezes… Tried a bunch of stuff, but I can’t seem to find what’s crashing unity. Maybe I just need to take my eyes off the code for a little bit. In the meantime, if anyone has any ideas and could lend a hand I’d really appreciate the help.

Here’s my code:

`   using UnityEngine;
    using System.Collections;
    using System.Collections.Generic;
    
    //Make sure there is always a rigidbody
    [RequireComponent (typeof (Rigidbody))]
    [RequireComponent (typeof (SphereCollider))]
    
    public class Planet_Controls : MonoBehaviour {
    	
    	// Key press vars
    	public float fRotationSpeed = 100.0F;
    	public float fFriction = 1;
    	public float fInterpolation = 1;
    	
    	// Drag rotate vars
    	public int iNumberAverages = 3;
    	private Quaternion qOriginalRot;
    	private Quaternion qOffsetRot;
    	
    
    	
    	// Use this for initialization
    	void Start () {
    	
    	}
    	
    	void Awake() {
    		iNumberAverages = Mathf.Clamp(iNumberAverages,1,iNumberAverages);
    	}
    	
    	void OnMouseDown() {
    		RaycastHit hit;
    		Vector3 dir;
    		
    		//Stop spinning
    		rigidbody.angularVelocity = Vector3.zero;
    		
    		//Record Initial Vars
    		if(Physics.Raycast (Camera.main.ScreenPointToRay(Input.mousePosition),out hit)){
    			qOriginalRot = transform.rotation;
    			dir = hit.point - transform.position;
    			qOffsetRot = Quaternion.Inverse (Quaternion.LookRotation(dir));
    			Spin(dir);
    		}
    	}
    	
    	void Spin (Vector3 dir) {
    		RaycastHit hit;
    		List<Vector3> previousDirList;
    		Vector3 currentDir = dir;
    		
    		previousDirList = new List<Vector3>();
    		
    		//init previousdir list
    		for (int i = 0; i < iNumberAverages; i++){
    			previousDirList.Add(currentDir);
    		}	
    		//Make the object rotate with the cursor while we are grabbing it
    		while (Input.GetButton("Interact") && Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition),out hit)) {
    			//Remove 1st element of array
    			previousDirList.RemoveAt(0);
    			
    			//Add currentdir to the end
    			previousDirList.Add(currentDir);
    			currentDir = hit.point - transform.position;
    			transform.rotation = Quaternion.LookRotation (currentDir) * qOffsetRot * qOriginalRot;
    		}
    		
    		//User let go of the mouse, so let it spin on its own
    		Vector3 avgPreviousDir = Vector3.zero;
    		
    		for (int i = 0; i < iNumberAverages; i++) {
    			avgPreviousDir += previousDirList*;*
  •  }*
    
  •  avgPreviousDir /= iNumberAverages;*
    
  •  Kick(currentDir,avgPreviousDir);*
    
  • }*

  • void Kick (Vector3 r2, Vector3 r1){*

  •  Vector3 linearVelocity;*
    
  •  //Calculate the angular velocity: omega = r x v / r^2*
    
  •  linearVelocity = (r2-r1) / Time.deltaTime;*
    
  •  rigidbody.angularVelocity = Vector3.Cross (r2, linearVelocity) / r2.sqrMagnitude;*
    
  • }*

  • // Update is called once per frame*

  • void Update () {*

  •  //Key press rotation system*
    
  •  	//Grab inputs*
    

_ float xrot = Input.GetAxis(“XRot”)*fRotationSpeed;_
_ float yrot = Input.GetAxis(“YRot”)*fRotationSpeed;_
_ float zrot = Input.GetAxis(“ZRot”)*fRotationSpeed;_

  •  	//Keep it rotating every frame*
    

_ xrot *= Time.deltaTime;_
_ yrot *= Time.deltaTime;_
_ zrot *= Time.deltaTime;_

  •  	//Apply rotation to object*
    
  •  	transform.Rotate(xrot,yrot,zrot);*
    
  • }*
    }`

while (Input.GetButton(“Interact”) && Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition),out hit)) {

will loop infinitely once its true, causing the crash.