Im instantiating a ragdoll prefab and I need it to have an initial velocity. I keep getting hit with null reference exceptions.
(the object is destroyed when the space bar is pressed)
public Rigidbody rag;
void OnDestroy()
{
rag = Instantiate(ragdoll, transform.position,transform.rotation) as Rigidbody;
rag.AddForce(100,100,100);//null reference at this line
}
Are you instantiating an entire ragdoll prefab? If so, it’s unlikely to have just a single rigidbody which controls the entire ragdoll. Try doing something like this, instead (without the public rigidbody, at the top)-
void OnDestroy()
{
Vector3 startingVelocity = rigidbody.velocity;
// Assuming you have a rigidbody on your character, otherwise work this out some other way
GameObject rag = Instantiate(ragdoll, transform.position, transform.rotation) as GameObject;
foreach(Rigidbody body in rag.GetComponentsInChildren<Rigidbody>())
{
body.velocity = startingVelocity;
}
}