Bullets disappear after small distance sometimes.

New to Unity. Very new. I am trying to make a script for my gun to fire bullets and have a muzzle flash when it does so. It works… but sometimes bullets just disappear after going a small (0.1-0.2 units on x) distance from the bullet spawn point. This mostly happens when I click fire quickly and not when I hold the fire button down. I wanted a muzzle flash whenever I shot a bullet and for it to disappear quickly after. Here’s the code:

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

public class Weapon : MonoBehaviour
{
public Transform FirePt;
public GameObject bulletPrefab;
public GameObject mFlash;
public float invokeDelay = 0.1f;
public float fireDelay = 0.16f;
float fireElapsedTime = 0;

void Start()
{
mFlash.SetActive(false);
}
void Update()
{
fireElapsedTime += Time.deltaTime;
if (Input.GetButton(“Fire1”) && fireElapsedTime >= fireDelay)
{
mFlash.SetActive(true);
Invoke(“Shoot”, invokeDelay);
}
}
void Shoot()
{
Instantiate(bulletPrefab, FirePt.position, FirePt.rotation);
fireElapsedTime = 0;
mFlash.SetActive(false);
}
}

Shot in the dark, but you say it mostly happens when you fire rapidly, could bullets collide with each other (i assume you have some scripts that removes them once they collide with something)?

Another possible issue I see (don’t have unity installed to verify) is that you set fireElapsedTime = 0 in invoked function, that means there is brief time (0.1f, invoke delay) where you can instantiate multiple bullets in a row if you click fast enough, I assume this is not intended

It happens when I shoot one bullet quickly. Like a tap. The bullet disappears quickly. When I hold down fire, it doesn’t happen. For values of invokeDelay other than 0.1f (both more and less than 0.1f), when I hold down fire, around half the bullets disappear quickly and half function normally.

“Another possible issue I see (don’t have unity installed to verify) is that you set fireElapsedTime = 0 in invoked function, that means there is brief time (0.1f, invoke delay) where you can instantiate multiple bullets in a row if you click fast enough, I assume this is not intended.”

Thanks, that worked.