C# Script Error

In my script i instantiate a bullet and then add a force to it but then when i click on the play button i gives me this error:

NullReferenceException: Object reference not set to an instance of an object
Gun.Shoot () (at Assets/Scripts/Gun.cs:69)
Gun.Update () (at Assets/Scripts/Gun.cs:47)

i even checked another script wich was exactly the same and i did work i can’t seem to find out why it’s not working so i need help with this one.

if (Input.GetKeyDown(KeyCode.Mouse0) && CanShoot == true)
{
Rigidbody shoot;
shoot = Instantiate(Bullet, BulletSpawn.transform.position, BulletSpawn.transform.rotation) as Rigidbody;
shoot.AddForce( transform.forward * BulletSpeed);

I don’t think you can cast ‘as Rigidbody’ here.
You’ll want to assign the instantiated object to another variable first. Then grab the Rigidbody component.
Kinda like this.

I’d also recommend renaming ‘shoot’ to something more substantial, like ‘bulletRigidbody’.

The other thing is you’ll want to make sure that ‘Bullet’ is actually assigned in the inspector as the prefab, same with the BulletSpawn- if you’re not already doing this.

// these two need to be assigned in the inspector
public GameObject Bullet; // Also I'd rename this to BulletPrefab
public GameObject BulletSpawn;

if (Input.GetKeyDown(KeyCode.Mouse0) && CanShoot == true) { 
    GameObject shootGameObject;
    Rigidbody shoot;

    shootGameObject = Instantiate(Bullet, BulletSpawn.transform.position, BulletSpawn.transform.rotation) as GameObject; // <-- cast as GameObject initially

   shoot = shootGameObject.GetComponent<Rigidbody>(); // <-- then grab the Rigidbody component and assign it.
   shoot.AddForce( transform.forward * BulletSpeed);
 }

------------ Below is how I’d write this script.

public bool CanShoot;
public float BulletSpeed;
public GameObject BulletPrefab;
public GameObject BulletSpawn;

if (Input.GetKeyDown(KeyCode.Mouse0) && CanShoot) { 
    var bulletGameObject = (GameObject)Instantiate(Bullet, BulletSpawn.transform.position, BulletSpawn.transform.rotation);

   bulletGameObject.GetComponent<Rigidbody>().AddForce(transform.forward * BulletSpeed * Time.deltaTime; // multiply by Time.deltaTime to get consistent motion
 }

Let me know if this works, good luck!

The other option is to instantiate the object at your bullet spawn point and then in the Bullet script itself just add the force directly to it on Awake();

public class PlayerShooting : MonoBehaviour {
    bool canShoot;
    GameObject bullet;
    Transform spawnPoint;

	void Update () {
        UpdateCanShoot();
        GetInputShoot();
	}

    private void GetInputShoot()
    {
        if (Input.GetButtonDown("Fire1"))
        {
            Instantiate(bullet, spawnPoint.position, Quaternion.identity);
        }
    }

    private void UpdateCanShoot()
    {
        //shoot condition
    }


}

public class Bullet : MonoBehaviour
{
    [SerializeField]
    static float shotForce;
    public void Awake()
    {
        GetComponent<Rigidbody>().AddForce(transform.forward * shotForce);
    }
}

Thank you for your answers but now it suddenly it works without me changing anything in the scripts… i dont know what is happening but it’s weird.