How to Instantiate an Item Properly?

Hey folks, I’m trying to make a beginner script in which I shoot something whenever I click. Right now I’m having a problem with the preceding “bullet” stops when I shoot another bullet (video here). I’m not sure how to fix it.

Here’s my code:

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

public class Test : MonoBehaviour
{

    bool Shoot = false;
    // Sets a public GameObject variable called bulletPrefab
    public GameObject bulletPrefab;
    // Sets a GameObject variable called bulletInstance
    private GameObject bulletInstance;
    // Start is called before the first frame update
    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {
        MouseClick();
    }

    public void MouseClick()
    {
        Shoot = true;
        if (Input.GetMouseButtonDown(0))
        {
            
            bulletInstance = Instantiate(bulletPrefab);
        }

        if (Shoot)
        {
            bulletInstance.transform.position += new Vector3(0, 0, 2 * Time.deltaTime);
        }
    }
}

That script is on my Gun GameObject (just an empty GameObject) and the public variable is my bulletPrefab (just a default sphere)

The bullet should have it’s own component that moves it, rather than being handled by this script.

1 Like

Basic game objects don’t really make good bullets as they don’t detect collisions. Ideally you should place a rigidbody component onto the bullet and enable interpolation.

You can then shoot the bullets with something like this:

using UnityEngine;
public class Test : MonoBehaviour
{
public Rigidbody bulletPrefab;
    void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            Rigidbody bullet=Instantiate(bulletPrefab,transform.position,transform.rotation);
            bullet.velocity=transform.forward*10;
        }
    }
}

You’re storing your bullet in the variable “bulletInstance” every time one is instantiated. This variable can only store 1 of your bullets at a time, so every time you instantiate a new bullet, the bullet stored in the variable is changed to your new bullet, halting the movement of your original one. I would add a new script to your prefab that gives your bullet constant movement in a certain direction. Something like this would probably work if you attached it to the bullet:

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

public class bulletmove : MonoBehaviour
{
    public float speed;
    void Update()
    {
        transform.Translate(transform.forward * speed * Time.deltaTime);
    }
}

as for the player, if all you want to do is move your bullet, then this should work

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

public class Test : MonoBehaviour
{
    public GameObject bulletPrefab;
    void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            Instantiate(bulletPrefab, transform.position, transform.rotation);
        }
    }
}