Unity freezes on play when I left click.

I had some help slowing down the ROF (rate of fire) on a gun, but when I go to shoot, unity freezes, and doesn’t come back. It happened after writing the script, so I am guessing it is a scripting problem. Here is the script:

public float delay = 0.5f;
 public float timeLeft = 0.5f;

void Shoot () {
		while (isFullAuto == true) {
			if (CurrentAmmo <= 0.5)
				break; 
			RaycastHit hit; 
			if (Physics.Raycast (WeaponCamera.transform.position, WeaponCamera.transform.forward, out hit, range)) {
				Debug.Log (hit.transform.name); 

				health = hit.transform.GetComponent<EnemyHealth> (); 
				if (health != null) {
					health.TakeDamage (damage); 
				}

				if (hit.rigidbody != null) {
					hit.rigidbody.AddForce (-hit.normal * hitForce); 
				}
				if (timeLeft > delay) {
					GameObject impactGO = Instantiate (impactEffect, hit.point, Quaternion.LookRotation (hit.normal)); 
					Destroy (impactGO, 2f);
				
					CurrentAmmo -= 1; 
					shotSound.Play ();
					timeLeft = delay;
			}
		}
	}
}

Where you call this Shoot() Method (In Update)? And are you sure your CurrentAmmo var is only change inside this while clause?
As I know since this Shoot() method called, your game will freeze for awhile until while clause breaks. So in my opinion your code should change to this (this just an example, I don’t know where you define CurrentAmmo var, as the naming it’s seem like properties so I let you define it yourself ) then leftclick mouse down, set the isShooting to true, mouse up, set the isShooting to false can solve your problem( I think) :

public class HandleShoot : MonoBehaviour{
    public bool isShooting;
    public float delay = 0.5f;
    public float timeLeft = 0.5f;

    void Update(){
        if (isShooting){
            Shoot();
        }
    }
 
    void Shoot () {
        if (!isFullAuto)
            return;
        if (CurrentAmmo <= 0.5)
            return;
        RaycastHit hit; 
        if (Physics.Raycast (WeaponCamera.transform.position, WeaponCamera.transform.forward, out hit, range)) {
            Debug.Log (hit.transform.name); 
 
            health = hit.transform.GetComponent<EnemyHealth> (); 
            if (health != null) {
                health.TakeDamage (damage); 
            }
 
            if (hit.rigidbody != null) {
                hit.rigidbody.AddForce (-hit.normal * hitForce); 
            }
            if (timeLeft > delay) {
                GameObject impactGO = Instantiate (impactEffect, hit.point, Quaternion.LookRotation (hit.normal)); 
                Destroy (impactGO, 2f);
                 
                CurrentAmmo -= 1; 
                shotSound.Play ();
                timeLeft = delay;
            }
        }
    }
}

Hope this help, Cheers !!! Vince

@JVLVince has provided a good fix within your design. You might want to reconsider it, though. It seems like a coroutine for shooting might be a little more natural to the problem you are trying to solve, the way you want to express your solution, and the constraints imposed by Unity.

Essentially, coroutines are cooperative threads. They use the enumeration mechanism built into C# and oh-so-cleverly coerce the “yield” keyword to mean “yield control from this thread”. Because of this design, you can do things like yield for a specific amount of time. So, with a coroutine, you could basically use your original algorithm and just yield an half-second delay after you shoot each bullet.

Further reading: Coroutines.