Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Destroy a component in C#

Discussion in 'Scripting' started by WinningGuy, Feb 4, 2010.

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

    WinningGuy

    Joined:
    Aug 10, 2009
    Posts:
    884
    Hi.

    First, forgive me for not knowing what I'm doing.

    With that said, how do I destroy a component in C#?

    I'm trying to make some alterations to the CombineChildren script so that the Mesh Renderer of all the child objects is destroyed once the game starts. I am using Unity's primitive game objects as colliders. I like having the mesh viewable in the editor so that I know where I've placed all my colliders. But I'd like them to disappear at runtime.

    The Unity docs are in Javascript, and the CombineChildren script is in C#.

    I know how to destroy a game object. But I don't know how to just single out components.
     
  2. h.istvan

    h.istvan

    Joined:
    Aug 4, 2009
    Posts:
    77
    You can destroy components similarly as GameObjects:
    Code (csharp):
    1. Destroy(gameObject.GetComponent("ComponentName"));
    Where ComponentName is the name of the component you want to destroy.
     
    StartStart likes this.
  3. AmazingInt

    AmazingInt

    Joined:
    Dec 7, 2009
    Posts:
    157
    You've got several options:
    1) Put the objects into a layer on their own and set the layer to non renderable
    2) Set the mesh renderer for each of the objects to disabled

    Are all the objects children of a parent? If so you can select them all and disable the mesh renderer with:


    http://forum.unity3d.com/viewtopic.php?p=247597
     
  4. WinningGuy

    WinningGuy

    Joined:
    Aug 10, 2009
    Posts:
    884
    Thanks for the link.

    Shortly after I started this thread I started getting rid of everything in CombineChildren that I didn't need and ended up with almost nothing except the table.

    But that solution in JS worked great.

    Code (csharp):
    1. function Start() {
    2.  
    3.  
    4.    var transforms = GetComponentsInChildren (Transform);
    5.       for (var t : Transform in transforms) {
    6.          if(t.gameObject.GetComponent(Renderer)){
    7.            if (t.gameObject.GetComponent(Renderer).enabled)
    8.            {
    9.               t.gameObject.GetComponent(Renderer).enabled = false;
    10.            }
    11.          }
    12.      
    13.    }
    14. }
     
  5. faraz

    faraz

    Joined:
    Aug 4, 2014
    Posts:
    46
    The public static function Destroy() is a key its Removes a gameobject, component or asset. reference


    Examples:

    // 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, it will remove the script
    // named FooScript from the game object
    function Update () {
    if (Input.GetButton ("Fire1") && GetComponent (FooScript))
    Destroy (GetComponent (FooScript));
    }
     
  6. BenZed

    BenZed

    Joined:
    May 29, 2014
    Posts:
    524
    I'd like to point out that the unity docs are not just in javascript. Theres an option at the top that allows you to switch the language the examples are shown in.
     
  7. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    This thread is way old.

    --Eric
     
Thread Status:
Not open for further replies.