I am trying to create a Distance2dJoint inside a script that extends MonoBehaviour.
Unfortunately, I am getting NullReferenceException exception on this line:
dj2d.connectedBody = otherBody;
What am I doing wrong? It seems that dj2d is null, but how can that be when I have instantiated it?
void OnTriggerEnter2D( Collider2D other )
{
CatController otherCatController = other.GetComponentInParent<CatController> ();
if ( otherCatController ) {
Debug.Log("Hit a cat");
if( otherCatController.index<index ){
DistanceJoint2D dj2d = new DistanceJoint2D();
Rigidbody2D otherBody = otherCatController.GetComponent<Rigidbody2D>();
dj2d.connectedBody = otherBody;
}
}
}