DateTime rocket launcher timing issue

Anyone know why this doesn’t work? It initially waits the 3 seconds to fire, but after that it’s failing to listen to this line:

**LastFire = DateTime.Now.AddSeconds(firerate);**

After the initial 3 seconds, it proceeds to vomit rockets. As far as I can tell LastFire is not being assigned, any ideas?

public class MissleLauncher : MonoBehaviour
{

    public Rigidbody projectile;
    public float firerate = 3;
    DateTime LastFire = DateTime.Now;

    float speed = 1f;

	// Use this for initialization
	void Start ()

`` {

	}




    void Fire()
    {
        if (LastFire.AddSeconds(firerate) <= DateTime.Now)
        {

            Rigidbody instantiatedProjectile;
            instantiatedProjectile = (Rigidbody)Instantiate(projectile, transform.position, transform.rotation);
            instantiatedProjectile.velocity = transform.TransformDirection(new Vector3(0, 0, 100));
            //instantiatedProjectile.position = transform.TransformDirection(transform.position + new Vector3(0, 0, 20));
            Physics.IgnoreCollision(instantiatedProjectile.collider, transform.collider);
            LastFire = DateTime.Now.AddSeconds(firerate);
        }
     
    }
	
	// Update is called once per frame
	void Update () 
    {
       
	}

Update:
I think your problem is not actually shown in your code above! I bet you are doing this:

Input.GetKeyDown(KeyCode.Space)

when you mean to be doing this:

Input.GetKey(KeyCode.Space)

Here’s my code and it works:

using System;
using UnityEngine;

public class FireRocket : MonoBehaviour
{
    public Rigidbody projectile;
    public float firerate = 3;
    DateTime LastFire = DateTime.Now;

    // Use this for initialization
    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetKey(KeyCode.Space))
        {
            Fire();
        }
    }

    void Fire()
    {
        if (LastFire.AddSeconds(firerate) <= DateTime.Now)
        {
            Rigidbody instantiatedProjectile;
            instantiatedProjectile = (Rigidbody)Instantiate(projectile, transform.position, transform.rotation);
            instantiatedProjectile.velocity = transform.TransformDirection(new Vector3(0, 0, 100));

            Physics.IgnoreCollision(instantiatedProjectile.collider, transform.collider);
            LastFire = DateTime.Now.AddSeconds(firerate);
        }
    }

}

Original Answer:
I think this line of code is your problem:

if (LastFire.AddSeconds(firerate) <= DateTime.Now)

This adds firerate seconds to the LastFire variable before comparing it to DateTime.Now, every time the Fire method is called.

So, I don’t have an explanation for this yet, but when I moved the DateTime assignment to the top of the Fire() block, it works like a charm. SO CONFUSED! If anyone knows what is going on here please let me know, I’d love to have my sanity back.

 public void Fire()
    {
        if(DateTime.Now.CompareTo(LastFire)>0)
        {
            LastFire = DateTime.Now.AddSeconds(firerate);
            BroadcastMessage("UpdateGUIText", LastFire.ToString());
            Rigidbody instantiatedProjectile;
            instantiatedProjectile = (Rigidbody)Instantiate(projectile, transform.position, transform.rotation);
            instantiatedProjectile.velocity = transform.TransformDirection(new Vector3(0, 0, 100));            
            Physics.IgnoreCollision(instantiatedProjectile.collider, transform.collider);           
            
        }
     
    }