FPS Shooting Script

I’m trying to create a basic FPS script that I thought I could handle, however I keep getting this error:

NullReferenceException: Object reference not set to an instance of an object
Fire.Shoot () (at Assets/Scripts/Fire.cs:24)
Fire.FixedUpdate () (at Assets/Scripts/Fire.cs:19)

The error pops up anytime I press the left mouse button in play mode.

Here is my shooting script called Fire:

using UnityEngine;
using System.Collections;

public class Fire : MonoBehaviour {

	public GameObject bullet;
	public GameObject shell;

	public GameObject firePoint;

	// Use this for initialization
	void Start () {
	
	}
	
	// Update is called once per frame
	void FixedUpdate () {
		if(Input.GetKeyDown(KeyCode.Mouse0)){
			Shoot();
		}
	}

	void Shoot () {
		Instantiate(bullet,transform.position, transform.parent.rotation);
		bullet.rigidbody.AddForce(bullet.transform.forward * 2000f);
	}
}

I have no idea what this error means or how to fix it. Any help is appreciated.

found your problem

Instantiate(bullet,transform.position, transform.parent.rotation);

should be

Instantiate(bullet.transform.position, transform.parent.rotation);

its bullet DOT transform, not bullet COMMA transform. The null reference error is referring to the fact the bullet has no transform from which to instantiate.

Typo at line 24.

Instantiate(bullet.transform.position, transform.parent.rotation);

Make sure you added a bullet in the inspector. Then you need to apply force to the instanced bullet, not the one held in the variable.

Instantiate creates a copy of the bullet your specify.

void Shoot()
{
    // Duplicate/copy bullet and store it in bulletInstance
    GameObject bulletInstance = Instantiate(bullet, transform.position, transform.parent.rotation) as GameObject;

    // AddForce to the copied bullet
    bulletInstance.rigidbody.AddForce(bullet.transform.forward * 2000f);
}

Instantiate() Takes 3 arguments. One of which needs to be the bullet prefab itself.