At first, I add the hingejoint 2d component to the gameobject. Then, I have a prefab, I instantiate it and want to assign its rigidbody 2d component to the connected body of the hingejoint 2d.
Iam unable to do this. I read all the documentation as well as the blogs. Please help.
Here’s the code.
//Boxnew is the prefab, I want its rigidbody component
GameObject box2 = (GameObject)Instantiate (boxnew);
//Now I attach hingejoint to my gameobject, works fine
hingenew = gameObject.AddComponent<HingeJoint2D>();
hingenew.GetComponent<HingeJoint2D>().connectedBody = box2.rigidbody2D;
//Error Above
//Error: InvalidCastException: Cannot cast from source type to destination type.
Iam unable to set the connected body at runtime. Literally gave up.