Search Unity

  1. If you have experience with import & exporting custom (.unitypackage) packages, please help complete a survey (open until May 15, 2024).
    Dismiss Notice
  2. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice

Destroying GameObjects from within their own script

Discussion in 'Scripting' started by Bampf, Nov 4, 2005.

  1. Bampf

    Bampf

    Joined:
    Oct 21, 2005
    Posts:
    369
    How should an object destroy itself? I tried
    Code (csharp):
    1.  
    2.     Destroy (this );
    but that didn't seem to work. Presumably that is just destroying the script component of the object. Is there a way to get to the parent GameObject?

    Thanks,
    Matt
     
  2. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
    Code (csharp):
    1.  
    2. Destroy(gameObject);
    3.  
    Btw. MonoBehaviour derives from Behaviour and from Component so all the functions in Component are also directly available in MonoBehaviours.