Remove component from script

Is there any way to remove component from Gameobject using script?

For example:

I add FixedJoint to player by script, connect object to it (for grabbing), and when I drop it I want to remove the FixedJoint (because, I can't just "disable" joint). How can I do it?


ANSWERS:


Yes, you use the Destroy function to destroy/remove a component from a GameObject. It can be used to remove Component or GameObject.

Add Component:

gameObject.AddComponent<FixedJoint>();

Remove Component:

FixedJoint fixedJoint = GetComponent<FixedJoint>();
Destroy(fixedJoint);

To experiment with Programmers correct answer, I created an extension-method so you can use gameObject.RemoveComponent(/* true if immediate */) because I felt there should a method like this.

If you'd want to use it you'd create a new class anywhere with the following code:

using UnityEngine;

public static class ExtensionMethods
{
    public static void RemoveComponent<Component>(this GameObject obj, bool immediate = false)
    {
        Component component = obj.GetComponent<Component>();

        if (component != null)
        {
            if (immediate)
            {
                Object.DestroyImmediate(component as Object, true);
            }
            else
            {
                Object.Destroy(component as Object);
            }

        }
    }
}

and then to use it like you'd do with AddComponent<>()

gameObject.RemoveComponent<FixedJoint>();

It will be accessible in any method that extends MonoBehaviour. You can also add more methods to this static extension class, just use the "this"-syntax as parameter to extend a certain Unity type. For instance if you add following method (from the extension method tutorial)

public static void ResetTransformation(this Transform trans)
{
    trans.position = Vector3.zero;
    trans.localRotation = Quaternion.identity;
    trans.localScale = new Vector3(1, 1, 1);
}

you would use transform.ResetTransformation(); in any of your scripts to invoke it. (Making the class look like:)

using UnityEngine;

public static class ExtensionMethods
{
    public static void RemoveComponent<Component>(this GameObject obj, bool immediate = false)
    {
        Component component = obj.GetComponent<Component>();

        if (component != null)
        {
            if (immediate)
            {
                Object.DestroyImmediate(component as Object, true);
            }
            else
            {
                Object.Destroy(component as Object);
            }

        }
    }

    public static void ResetTransformation(this Transform trans)
    {
        trans.position = Vector3.zero;
        trans.localRotation = Quaternion.identity;
        trans.localScale = new Vector3(1, 1, 1);
    }
}


 MORE:


 ? HTML- HREF Tag inside a Button
 ? lazyRegister: max possible with nested elements or iron pages?
 ? How to position <paper-input>s horizontally
 ? How do I modify the value of a custom global style using Polymer?
 ? Firebase Analytics Filter By User ID
 ? Return value from valueEventListener java
 ? Simple PHP contact form with Firebase hosting
 ? Retrieving user data from Firebase
 ? Need Firebase Database behaviour clarification when inside a Service
 ? Why the key of the previous child in order is returned on ChildEventListener()?