Rigitbody2D.velocity isn't working!!!

Can some explain me what is going on? )))
Debug.Log(rd.velocity); - is always 0,0 and bullet is not moving…

 public GameObject bullet;
 private Rigidbody2D rd;
 private Transform spawnPos;

    void Start () {
         rd = bullet.GetComponent<Rigidbody2D>();
         rd.velocity = new Vector2(4, 5) * 200f;
         spawnPos = transform.Find("SpawnBullets");
    }
	void Update ()
    {
        if (....)
        {
              Instantiate(bullet, spawnPos.position, spawnPos.rotation);
             Debug.Log(rd.velocity);
        }
   }

I’m guessing that you arent shooting bullets every frame, so that there’s some kind of ButtonDown in the if statement. The problem here is that the if statement will only be called once in that situation. Also you are instantiating a bullet, but not moving it at the moment of creation. The correct way to instantiate a bullet is something like this:

public GameObject bullet;
private Transform spawnPos;
public float force;

void Start(){
spawnPos = transform.Find("SpawnBullets");
}

void Update(){
if(...){
GameObject copyOfBullet = (GameObject) Instantiate(bullet, spawnPos.position, spawnPos.rotation);
copyOfBullet.GetComponent<Rigidbody>().AddForce(Vector3.forward * force);
}

What’s happening here is that you create a copy of your bullet to shoot out. Then access the rigidbody of your copy, not the original, and add force in the direction you want (times the force you want).

To now get the velocity of that bullet, you have to rearrange some code to access that instantiation.

public GameObject bullet;
private Rigidbody nextInstantiatedBullet;
private Transform spawnPos;
public float force;

void Start(){
spawnPos = transform.Find("SpawnBullets");
}

void Update(){

if(nextInstantiatedBullet != null){
     Debug.Log(nextInstantiatedBullet.velocity);
}

if(...){
GameObject copyOfBullet = (GameObject) Instantiate(bullet, spawnPos.position, spawnPos.rotation);
Rigidbody bulletRigidbody= copyOfBullet.GetComponent<Rigidbody>();
nextInstantiatedBullet = bulletRigidbody;

bulletRigidbody.AddForce(Vector3.forward * force);
}

By creating a variable to store the rigidbody in when it is created, we can now access that rigidbody velocity every frame, instead of only once when it is created. I hope this gives you an idea on what is the way to go. A little summary:

  1. Click Button
  2. Create a variable to store a copy of your bullet in.
  3. Instantiate a new bullet and assign it to your new variable.
  4. Add force to the rigidbody of the new instantiated bullet.