Gun script - Burst fire (Need help)

Hi guys
I’m pretty new to Unity and coding. I am trying to create a gun script for a gun, though I cannot seem to get the burst fire to work. I want it to shot three bullets in a burst when holding the mouse button down. When three bullets have been fired, the user must release the button and press again. Any ideas on what I am doing wrong?

I can give you the full gun script if you need it.

function FireBurst(){
    	ammoText.text="Ammo: "+ammo;
    	if(shoot==true){
    		if(Input.GetMouseButton(0) && Time.time > nextFire) {
    			while(burstCount!=3){
    				burstCount=burstCount +1;
    				BulletSpawn.light.intensity=1;
    				nextFire=Time.time+fireRate;
    				Instantiate(Bullet,BulletSpawn.transform.position,BulletSpawn.transform.rotation);
    				BulletSpawn.audio.Play();
    				ammo = ammo -1;
    				if(ammo <=30){
    					ammoText.color=Color.green;
    				}
    				if(ammo <=20){
    					ammoText.color=Color.yellow;
    				}
    				if(ammo <=10){
    					ammoText.color=Color.red;
    				}
    				if(Input.GetMouseButton(1)){
    				} else {
    					animation.Play("GunRecoil");
    				}
    			}
    		}
    	}
    }

The problem is your loop… It will happen and do all it’s code before exiting the if statement. without ever waiting on that timer again.

Here’s what it’s doing :
If you’re pressing the mouse, and the firerate is reached, enter a loop to fire 3 bullets instantaneously then get out of the if statement.

What you need to do is actually get them to be executed with a few frames of interval. there’s a few ways to go about this. Here’s what I would do :

Have a coroutine that will deal with firing, and deny firing while already firing. here’s what it’d look like :

function FireBurst()
	{
		ammoText.text="Ammo: "+ammo;
		if(shoot==true)
		{
			if(Input.GetMouseButton(0) && !firing) 
			{
				ShootInBurst();
			}
		}
	}
	
	function ShootInBurst()
	{
		firing = true;
		while (burstCount != 3)
		{
			burstCount=burstCount +1;
			BulletSpawn.light.intensity=1;
			Instantiate(Bullet,BulletSpawn.transform.position,BulletSpawn.transform.rotation);
			BulletSpawn.audio.Play();
			ammo = ammo -1;
			if(ammo <=30){
				ammoText.color=Color.green;
			}
			if(ammo <=20){
				ammoText.color=Color.yellow;
			}
			if(ammo <=10){
				ammoText.color=Color.red;
			}
			if(Input.GetMouseButton(1)){
			} else {
				animation.Play("GunRecoil");
			}
			yield WaitForSeconds(fireRate);
		}
		firing = false;
	}