How do i use AddForce on rigidbody ? The AddForce is not exist

I created a _rigidbody and used GetComponent so now the _rigidbody have AddForce but i need the AddForce to be part of the rigidboady of the shot variable.

using UnityEngine;
using System.Collections;

public class Shooter : MonoBehaviour {

    public GameObject bullet = null;
    public float bulletSpeed = 500f;

    private Rigidbody _rigidbody;

    void Update ()
    {
     if (Input.GetKeyDown(KeyCode.F))
        {
            GameObject shot = (GameObject)Instantiate(bullet, transform.position
                + (transform.forward * 2), transform.rotation);

            _rigidbody = GetComponent<Rigidbody>();
           shot.rigidbody.
          
        }
    }
}
_rigidbody = GetComponent<Rigidbody>();
//should be
_rigidbody = shot.GetComponent<Rigidbody>();

// then
_rigidbody.AddForce( //... etc.

without a specified gameobject, getcomponent looks for a component on the same gameobject the script is attached to.

1 Like