Multiple joints on GameObject
Hi Guys,
I am trying to add multiple joints to an object on collision. But it only Adds one joint to each object. So for example if there is a central sphere with 6 other neighbouring spheres the central sphere should have 6 joints connected to it.
This is the bit of code that I have. It seems to be creating the remaining 5 joints, but in the inspector it says that no rigid body is attached to the created joint. There is only 1 joint that displays a connected rigid body.
Any help would be great!, thanks
private void OnCollisionEnter(Collision collision)
{
if (AP.addSpringJoints)
{
AddFixedJoint(collision);
}
}
private void AddFixedJoint(Collision data)
{
gameObject.AddComponent<FixedJoint>();
gameObject.GetComponent<FixedJoint>().connectedBody = data.rigidbody;
gameObject.GetComponent<FixedJoint>().breakForce = Mathf.Infinity;
}
Answer by UnityCoach · Oct 08, 2018 at 02:46 PM
GetComponent will always return the first FixedJoint you added. Try this :
private void AddFixedJoint(Collision data)
{
FixedJoint joint = gameObject.AddComponent<FixedJoint>();
joint.connectedBody = data.rigidbody;
joint.breakForce = Mathf.Infinity;
}
Very nice, I didn't know that about GetComponent. Thanks!
Your answer
![](https://koobas.hobune.stream/wayback/20220612180002im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
how do i collide and kill the enemy while pressing space Unity 5 C# 2 Answers
Collision Detection between two different Prefabs doesnt work 1 Answer
If Statement not working properly - Comparing game objects 0 Answers
Invisible objects appear after collision 2 Answers
Unity physics Help Implementing Real Life Physics in Jumping System C# 2 Answers