Problem with instatiating objects

I’m trying to create a bullet via the usage of a prefab and then adding force it.
It doesn’t matter how i try to make it work , I keep getting a nullReference error.

The code:

    void shoot(){
        if (Time.time >= coolDown) {
            GameObject bPrefab = Instantiate(bulletPref,this.transform.position,Quaternion.identity) as GameObject;
            bPrefab.GetComponent<Rigidbody2D>().AddForce(transform.forward * velocity);


            coolDown = Time.time + attackSpeed;
        }

Is bulletPref null? Can you post the full error message?

Either that or there is no Rigidbody2D attached.

1 Like

Worked it around this way

            Rigidbody2D bPrefab = Instantiate (bulletPref, this.transform.position, Quaternion.identity) as Rigidbody2D;
            bPrefab.GetComponent<Rigidbody2D>().AddForce(transform.up * bspeed);
            coolDown = Time.fixedTime + attackSpeed;