Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.

Add and remove components by scripting

Discussion in 'Scripting' started by xcoder, Oct 29, 2009.

Thread Status:
Not open for further replies.
  1. xcoder

    xcoder

    Joined:
    Sep 26, 2009
    Posts:
    7
    Hi All,
    I am new to unity, in my project I want to add the physics to the objects at run-time and remove. I know the method for adding the component, but I don't know how to remove the component. Please suggest the way to do this.
     
    xCyborg, ivaylo5ev and SaSha_K like this.
  2. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,342
    Use Destroy() for removing components.

    --Eric
     
    MilitaryG, adasroky, dalekvan and 6 others like this.
  3. xcoder

    xcoder

    Joined:
    Sep 26, 2009
    Posts:
    7
    Thank You
     
    LuisAlfredo92 and AlexTuduran like this.
  4. rocksean30

    rocksean30

    Joined:
    Oct 11, 2011
    Posts:
    1
    // Kills the game object
    Destroy (gameObject);

    // Removes this script instance from the game object
    Destroy (this);

    // Removes the rigidbody from the game object
    Destroy (rigidbody);

    // Kills the game object in 5 seconds after loading the object
    Destroy (gameObject, 5);

    // When the user presses Ctrl or Left Mouse Button, it will remove the script
    // named myScript from the game object
    function Update () {
    if (Input.GetButton ("Fire1") GetComponent (myScript ))
    Destroy (GetComponent (myScript ));
    }
     
  5. Seizure

    Seizure

    Joined:
    Dec 18, 2012
    Posts:
    9
    In editor mode use DestroyImmediate();
     
  6. Deleted User

    Deleted User

    Guest

    Problem though is when you wish to destroy a component in order to recycle the object and then when you re recycle the object in a different time you wish to add that deleted component again ( at runtime).Destroy doesn't remove the entry or instanceId if you will of that component on the collection that stores references to all of the gameobject's components.On the editor that works well because the RemoveComponent of the Editor is C++ code whereas runtime is C# scripting and apparently scripts don't have access to that collection.
     
    gusals951753 and Demigiant like this.
  7. angrypenguin

    angrypenguin

    Joined:
    Dec 29, 2011
    Posts:
    14,650
    It's a C# call to a C++ engine function either way.

    If you want to "recycle" stuff as you mentioned you shouldn't be using Destroy() at all. The component(s) to be recycled need to be on their own GameObject (because the Transform is how its relationship with the scene is managed), and instead of destroying you should be disabling and re-enabling it, and probably changing what it's parented to.
     
  8. siddharth3322

    siddharth3322

    Joined:
    Nov 29, 2013
    Posts:
    950
    for removing component this works

    Destroy (rigidbody);
     
  9. faraz

    faraz

    Joined:
    Aug 4, 2014
    Posts:
    46
    Thanks man it worked
     
    AlexTuduran likes this.
  10. gokulreddy

    gokulreddy

    Joined:
    Apr 2, 2014
    Posts:
    11
    Then how to add Components in runtime
     
  11. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,796
    With AddComponent
     
    AlexTuduran and bluny00 like this.
  12. gokulreddy

    gokulreddy

    Joined:
    Apr 2, 2014
    Posts:
    11
    Hi,
    I have an issue with reloading a small ball,when i hit on the blue line ball gets detached but i need to get small ball reloaded with a small delay so that i should get a real feel of ball is detaching and new ball comes in.
    Thanks in advance

    20150518105526.jpg
     
  13. TheAutoGamer

    TheAutoGamer

    Joined:
    Sep 28, 2020
    Posts:
    2
  14. xCyborg

    xCyborg

    Joined:
    Oct 4, 2010
    Posts:
    614
    Cool, thanks.
     
  15. HannibleJester

    HannibleJester

    Joined:
    Jan 15, 2021
    Posts:
    9
    i actually have an issue where im not allowed to disable certain components like the checkbox is literally missing
     
Thread Status:
Not open for further replies.
unityunity