Auto fire in Update()

Hi everyone! I’m beginner and i have a question in my code:

void Fire () {
        GameObject beam = Instantiate(projectile, new Vector3(transform.position.x, transform.position.y, transform.position.z), Quaternion.identity) as GameObject;
        beam.GetComponent<Rigidbody2D>().velocity = new Vector3(0, projectileSpeed, 0);
        AudioSource.PlayClipAtPoint(fireSound, transform.position);
    }
   
void Update () {
        Fire();
}

Time between 2 projectiles is too small so projectiles are created too much and my game will be crashed. How can i fix that? If i use Input.GetKey in Update my game seems to be good, time between 2 projectiles is sensible and my game runs smoothly:

void Update () {
        if (Input.GetKeyDown (KeyCode.Space)) {
            InvokeRepeating ("Fire", 0.000001f, firingRate);
        } else if (Input.GetKeyUp (KeyCode.Space)) {
            CancelInvoke("Fire");
        }
}

[/code]

You are using InvokeRepeating meaning it will repeat the “FireCode” every 0.000001 seconds which is very fast and if you don’t lift the key up for a secnd or two can create loads of bullets which may be ur problem , instead of having the invoke repeat in the if statement, just have

void Update () {
        if (Input.GetKeyDown (KeyCode.Space)) {
            Fire();
        } else if (Input.GetKeyUp (KeyCode.Space)) {
            CancelInvoke("Fire");
        }
}