Your code, where you get a NullReferenceException
in the last line:
Rigidbody shotProj = Instantiate(
chosenProj, transform.position, transform.rotation)
as Rigidbody;
shotProj.AddForce(frontDir);
In the last line something must be null
, or otherwise you wouldn't get the exception. Since frontDir
is a Vector3
value type, the only reference type that can be null
is shotProj
.
How could it be null
? Well, when the return value of Instantiate()
cannot be cast to a Rigidbody
, the as Rigidbody
expression will return null
.
So, I conclude that your chosenProj
is not a RigidBody
. It is actually a GameObject
that has a rigid body component on it. To get the RigidBody
, use this:
GameObject shotProj = (GameObject)Instantiate(chosenProj, transform.position, transform.rotation);
shotProj.rigidbody.AddForce(frontDir);
GameObject
class documentation has more info on how to get components from game objects.