Why does the first line of code work inside the OncollisionEnter event and not the second.
OnCollisionEnter(collision : Collision)
{
1: rigidbody.position = collision.rigidbody.position; //// OK!
2: fixedjoint.connectedBody = collision.rigidbody; //// BAD!
}
The object the script is attached to has both a rigidbody and a fixedjoint component.
Error message I get is : An instance of type ‘UnityEngine.Joint’ is required…
Why isn’t and instance of type rigidbody required?
Unity provides some short cuts to common components. When we do:
rigidbody.velocity = Vector3.zero;
…that is really a short cut to:
gameObject.GetComponent(Rigdibody).velocity = Vector3.zero;
… and in fact, Unity is doing the GetComponent() each time we reference a rigidbody.
If you look at the reference page for GameObject, you will see the variables that have shortcuts. ‘fixedJoint’ is not one of them. So you would have to do:
gameObject.GetComponent(FixedJoint).connectedBody = collision.rigidbody;