'Add Component to GameObject that is locally safed

I want to safe a configurable joint as local variable, destroy it than (do stuff) add the configurable joint.

        Joint joint = outsideBodyPart.GetComponent<ConfigurableJoint>();
        Destroy(outsideBodyPart.GetComponent<ConfigurableJoint>());
        Debug.Log("do Stuff");
        outsideBodyPart.AddComponent<ConfigurableJoint>() = joint;

That doesnt't work, any suggestions friends?



Solution 1:[1]

You are already saving the Joint in a variable so use it instead of calling it every time.

also, why do you use the Joint variable type when you want the ConfigurableJoint component?

    ConfigurableJoint joint = outsideBodyPart.GetComponent<ConfigurableJoint>();
    Destroy(outsideBodyPart.GetComponent<ConfigurableJoint>());
    Debug.Log("do Stuff");
    outsideBodyPart.AddComponent(typeof(ConfigurableJoint)) as ConfigurableJoint

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 amitklein