Can't apply AddForce after Instantiating object

I’ve been trying out different methods for a shooting script to try and make it as versatile as possible.

I’ve decided on something like this:

    public GameObject bullet;
	public float bulletSpeed = 1000f;
	private Rigidbody bulletRB;

	void Update () 
	{
		if (Input.GetMouseButtonDown (0)) 
		{
			GameObject.Instantiate (bullet, this.transform.position, this.transform.rotation);
			bulletRB = bullet.GetComponent <Rigidbody> ();
			bulletRB.AddForce (this.transform.forward * bulletSpeed);
		}
	}

But when the object is created it just falls without any force. I know it has something to do with how I’m applying the force, but I can’t figure it out. Any help would be awesome!

It looks like you’re trying to add force to the prefab of your bullet object and not the instantiated object.

Try something like this:

public GameObject bullet;
public float bulletSpeed = 1000f;

void Update()
{
    if (Input.GetMouseButtonDown(0))
    {
        GameObject bulletInstance = Instantiate(bullet, transform.position, transform.rotation);
        bulletInstance.GetComponent<Rigidbody>().AddForce(transform.forward * bulletSpeed);
    }
}