Home > Blockchain >  Why does my bullet lack a rigidbody even though it has one?
Why does my bullet lack a rigidbody even though it has one?

Time:11-29

So I'm making a FPS game and when I try to shoot, it says my bullet lacks a rigidbody. I have a a rigidbody, so why is it like this?

Here's the bit of code that makes the bullet :

Vector3 aimDir = (transform.forward).normalized;
Instantiate(gunData.bulletPrefab, muzzle.position, Quaternion.LookRotation(aimDir, Vector3.up));
Rigidbody bulletRb = gunData.bulletPrefab.GetComponent<Rigidbody>();
bulletRb.velocity = aimDir * gunData.bulletSpeed;

CodePudding user response:

You seem to be trying to access the bulletPrefab's rigid body component instead of the newly instantiated object's.

try something like this:

var myNewBulletInstance = Instantiate(....
myNewBulletInstance.GetComponent<Rigidbody>();

CodePudding user response:

now It works. I also had to create a prefab variant to put the rigidbody on. thanks!

  • Related