Variable doesn't reset between test session. Bug?

I’ve got a very basic system set up with a delay between shots being fired, it saves the time the last shot was fired then checks if a delay + last fired > time.time. Pretty standard stuff copied from a tutorial. I’ve also tried calculating doing it the other way round calculating the time.time+delay then checking if time.time > this to no avail.

What happens is the first time I run the code in test mode in unity it works as you’d expect. However the second time the code is tested it carries over the lastFired value from the previous time I tested it. For example, I test the game, fire a few shots and my last shot before stopping the test is 4 seconds. I then start the test again and I can see (through debug) that the last fired time is 4 seconds, even if I’ve not fired a shot. After the 4s has elapsed I can fire again, and whatever it was when I close the test carries over to the next test.

My set up is as follows:

I have a Player Fire script on the player object with this method:

public class PlayerFire : MonoBehaviour {

    public Transform fireTransform;   
    public GameObject defaultWeapon;    
    private IWeapon activeWeapon;  

    void Start ()
    {
        activeWeapon = defaultWeapon.GetComponent<IWeapon>();
	}
	
	void Update () {
		if(Input.GetButtonDown("Fire1"))
        {
            Shoot();
        }
	}

    private void Shoot()
    {
        activeWeapon.Shoot(fireTransform);
        return;
    }
}

I have a rocket launcher prefab which at the moment is an empty object with a rocket launcher script attached. The Rocket launcher script implements a basic interface (IWeapon) that currently just requires a shoot() method:

public class RocketLauncher : MonoBehaviour, IWeapon
{
    public float fireDelay = 0.5f;      // delay (s) between shots

    public Rigidbody projectile;        // object of projectile to fire
    public AudioSource fireAudio;       // audio source
    public AudioSource fireClip;        // audio to play when fired

    public float launchForce = 30f;                 // default force that projectile is launched with

    private float timeSinceLastShot = 0;

    public void Shoot(Transform fireTransform)
    {
        if (Time.time > fireDelay + timeSinceLastShot)
        {
            //  instantiate shell object and set at orgin point/rotation
            Rigidbody shellInstance = Instantiate(projectile, fireTransform.position, fireTransform.rotation);

            // set velocity
            shellInstance.velocity = launchForce * fireTransform.forward;

            // Calculate time when weapon can next be fired
            timeSinceLastShot = Time.time;
        }
        Debug.Log("shoot" + Time.time + " nf:" + timeSinceLastShot);
    }
}

I can’t figure out why the variable (timeSinceLastShot would remain the same between tests (Clicking play button in editor), especially as it’s set to 0f in it’s declaration in the method.

I guess that you don’t work with an instance of your RocketLauncher prefab but with the prefab itself. That means you directly change use / change the asset in your project. Prefabs are meant as templates which should be instanciated.

So, no, it’s not a bug, you’re using the prefabs wrong ^^.