- Home /
Breaking joints
So I have this:
void OnCollisionEnter(Collision c)
{
var joint = gameObject.AddComponent<FixedJoint>();
joint.connectedBody = c.rigidbody;
}
and its tied to the object I am picking up but I want to be able to delete this joint when I press a key so I can drop the object I picked up with
Destroy (joint);
would I put that in the same class in the Update() method?
Answer by SkaredCreations · Dec 14, 2014 at 04:50 PM
Declare the variable as class member instead of locally in the function, so it'll be null by default and assigned in OnCollisionEnter, then when you'll need to delete you'll call Destroy(joint) and set the variable back to null.
I tried doing var joint = gameObject.AddComponent();
under my class definition but it said that wasnt the right way to declare public vars?
You have only to declare as class member, but then you have to assign it only in OnCollisionEnter:
public class Test : $$anonymous$$onoBehaviour {
FixedJoint joint;
void Update ()
{
if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.Delete)) {
if (joint != null) {
Destroy(joint);
joint = null;
}
}
}
void OnCollisionEnter(Collision c)
{
joint = gameObject.AddComponent<FixedJoint>();
joint.connectedBody = c.rigidbody;
}
}
hm its still staying connected though it is entering the if statements correctly
What means "staying connected"? Does it destroy and recreate or don't destroy at all? $$anonymous$$aybe it's entering OnCollisionEnter also after you destroy the joint.
Your answer
Follow this Question
Related Questions
2D 360 degress platformer example needed 0 Answers
More Realistic Physics 1 Answer
Run some code just after physics is calculated 1 Answer
bow and arrow game 6 Answers
Setting Wheelcollider BrakeTorque to 0 does not release brake 0 Answers