Rate of Fire won't change,Rate of Fire not changing rate of fire

,Most of this code was taken from this video: [Shooting with Raycasts][1] and it works pretty well except for the rate of fire doesn’t change no matter what the value of fireRate is even if I set it to a huge number like 10,000 or 1 the fire rate stays the same, I was wondering if anyone could help me find out why this was happening. Thanks in advance.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Gun : MonoBehaviour {
public float damage = 10f;
public float range = 100f;
public Camera fpsCam;
public ParticleSystem muzzleFlash;
public AudioSource gunshot;
public float fireRate = 1f;
public GameObject Impact;

public float Ammo;
public float Bullets;
private float AmmoUsed;
public float Score = 0f;

private float nextTimeToFire = 0f;

	// Use this for initialization
	void Start () 
	{
		Ammo = Bullets;

	}
	
	// Update is called once per frame
	void Update () 
	{
		

		Ammo = Bullets;

		if (Input.GetKeyDown(KeyCode.R))
		{
			Reload();
		}
			if (Input.GetButton("Fire1") && Time.time >= nextTimeToFire )

			{
				AmmoUsed ++;
				nextTimeToFire = Time.time + 1f/fireRate;
				
				gunshot.Play();
				
				Shoot();
			}




	}

// this makes the gun shoot
	void Shoot ()
	{
		muzzleFlash.Play();
		
		Bullets = Bullets - AmmoUsed; 
		Ammo = Bullets;
		
		// when it hits B)
			RaycastHit hit;
			if (Physics.Raycast(fpsCam.transform.position, fpsCam.transform.forward, out hit, range))
			{
				Debug.Log(hit.transform.name);


				EnemyHealth EnemyHealth = hit.transform.GetComponent<EnemyHealth>();
				// makes sure we're hitting something with EnemyHealth script, if target doesn't have the script, nothing here happens.
				if (EnemyHealth != null)
				{
					EnemyHealth.TakeDamage(damage);
					Score ++;
					gunshot.Play();
					GameObject bloodGO = Instantiate (Impact, hit.point, Quaternion.LookRotation(hit.normal));
					Debug.Log("Score is " +  Score);
				}

				// bullet impact
				GameObject impactGO = Instantiate (Impact, hit.point, Quaternion.LookRotation(hit.normal));
				Destroy(impactGO, 2f);
			}

	}

// reload
	void Reload ()
	{
		Bullets = 300000;
		AmmoUsed = 0;
		
	}
}

Script looks ok :slight_smile: At least the part you are worrying about.
Where are you changing the fireRate value? If you are changing it in script, recompile game and than play test it, sure, it wont’t work. Nothing will change.

You have to change the value in the inspector. (I suppose that’s the case because logic seems ok)

If you once created public member, it will be serialized by Unity and changing it in the declaration won’t do anything, because Unity will use the serialized value.

Bonus:
You play the gunshot sound twice (in Update method on shot, and when you hit an enemy).

Be aware that this script will never shoot more than once per frame so the maximum fire rate would be around 60 shots per second in most scenarios. Changing it to 10,000 wouldn’t change anything compared to 60 but it should make a difference compared to anything slower.