Bullet stops when new bullet instantiates pls help

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

public class ShootPistol : MonoBehaviour {
    float timeBetweenShots;
    bool canShoot;
    public GameObject bulletPrefab;
    GameObject bullet;
    Rigidbody bulletRB;
    const float bulletSpeed = 50;
	// Use this for initialization
	void Start () {
        canShoot = true;
        bullet = null;
	}
	
	// Update is called once per frame
	void Update () {
        timeBetweenShots += Time.deltaTime;
        if (timeBetweenShots < 1.5)
            canShoot = false;
        
        else
            canShoot=true;

        if (Input.GetMouseButtonDown(0) && canShoot)
        {
            bullet = Instantiate(bulletPrefab, GameObject.Find("BulletSpawn").transform.position,transform.rotation);
        }
        if (bullet != null)
        {
            bullet.transform.Translate(Vector3.forward * Time.deltaTime * bulletSpeed);
        }

	}
}

Hello.

In your update, you Translate your variable bullet.
But each time you instantiate a bullet, you replace the previous one.

Your update only update the very last bullet.

You should an other script to translate.
Or keep a list of bullets.

Good luck.

You could also:

  • Add a script to the bulletprefab that moves it forward every update, so every bullet has its own moving script.
  • Add a kinematic rigidbody to the bullet and set its velocity.

As mikilo and robot_rover said, you can make another script for bullet to move.

using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class ShootPistol : MonoBehaviour {
     float timeBetweenShots;
     bool canShoot;
     public GameObject bulletPrefab;
     GameObject bullet;
     Rigidbody bulletRB;
     const float bulletSpeed = 50;
     // Use this for initialization
     void Start () {
         canShoot = true;
         bullet = null;
     }
     
     // Update is called once per frame
     void Update () {
         timeBetweenShots += Time.deltaTime;
         if (timeBetweenShots < 1.5)
             canShoot = false;
         
         else
             canShoot=true;
 
         if (Input.GetMouseButtonDown(0) && canShoot)
         {
             bullet = Instantiate(bulletPrefab, GameObject.Find("BulletSpawn").transform.position,transform.rotation);
         } 
     }
 }

And other one on bullet:

using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class bulletMove : MonoBehaviour {
          const float bulletSpeed = 50;
     // Update is called once per frame
     void Update () {
      
             transform.Translate(Vector3.forward * Time.deltaTime * bulletSpeed);
         } 
 }