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 gameObject with tag

Discussion in 'iOS and tvOS' started by Moonjump, Oct 24, 2010.

  1. Moonjump

    Moonjump

    Joined:
    Apr 15, 2010
    Posts:
    2,572
    I'm sure I saw this a few months ago, but I cannot find it with searches, and I cannot get the right syntax.

    I want to delete a gameObject that has a particular tag. At the time I do it, I know there will be 1 object with that tag. I thought it was something like:

    Code (csharp):
    1. Destroy (gameObjectWithTag("gameMenu"));
    Edit: Sorry, I posted then realised it should be in the scripting forum.

    Edit 2: Solved, I'd had the right answer before, but a spelling mistake meant it hadn't worked, so I'd tried everything else. The correct format is:

    Code (csharp):
    1. Destroy (GameObject.FindWithTag("gameMenu"));
     
    Last edited: Oct 24, 2010
  2. bawi

    bawi

    Joined:
    Aug 12, 2011
    Posts:
    16
    thanks man, it's very helpful :)
     
  3. byshykov

    byshykov

    Joined:
    Nov 12, 2016
    Posts:
    1
    Destroy(gameObjectWithTag("gameMenu"));
    Спасибо ! Это работает для уничтожения старого объекта ( сейчас как раз RPG )типа Diablo делаю, и мне как-раз понадобилось создавать и подсвечивать новую target position и уничтожать старую target position
     
  4. RenatasM

    RenatasM

    Joined:
    Feb 4, 2017
    Posts:
    1
    Tai kad zinok niekas cia taves nesupranta. Ar tu is mezdio iskritai ir nematai, kad cia angliksai raso?
     
    Salciunas1 likes this.
  5. VertilogyMC

    VertilogyMC

    Joined:
    Dec 25, 2017
    Posts:
    2
    How do you destroy a simple cube?
     
  6. jazib_shahzad

    jazib_shahzad

    Joined:
    Feb 4, 2017
    Posts:
    2
    Destroy(this.gameobject);
     
  7. brushevchenko1

    brushevchenko1

    Joined:
    Nov 19, 2018
    Posts:
    1
    Thank U my dear, I needed that so much!!
     
  8. labana2019

    labana2019

    Joined:
    Jun 28, 2017
    Posts:
    2
    thanks bro for this
     
  9. DiscBloxer2

    DiscBloxer2

    Joined:
    Sep 2, 2020
    Posts:
    1
    How Do i Destroy Every Gam,e Object with The SameTag
     
  10. Moonjump

    Moonjump

    Joined:
    Apr 15, 2010
    Posts:
    2,572
    I set up a function that can be called from elsewhere:

    Code (CSharp):
    1.     void Update ()
    2.     {
    3.         if (condition)
    4.         {
    5.             DestroyWithTag ("myTag");
    6.         }
    7.     }
    8.  
    9.     void DestroyWithTag (string destroyTag)
    10.     {
    11.         GameObject[] destroyObject;
    12.         destroyObject = GameObject.FindGameObjectsWithTag(destroyTag);
    13.         foreach (GameObject oneObject in destroyObject)
    14.             Destroy (oneObject);
    15.     }
     
    mogutaru and Frazer88 like this.
  11. abyboyganteng

    abyboyganteng

    Joined:
    Jan 24, 2021
    Posts:
    1
    Thanks, man, very useful
     
  12. crykIer

    crykIer

    Joined:
    Feb 16, 2021
    Posts:
    1
    Thank you this was very helful
     
  13. Lokesh2022

    Lokesh2022

    Joined:
    May 8, 2021
    Posts:
    9
    thank you it's very useful
     
  14. idoknow68

    idoknow68

    Joined:
    Sep 13, 2022
    Posts:
    2
    thank you
     
  15. logee48

    logee48

    Joined:
    Apr 20, 2021
    Posts:
    1
    u found answer in 2010, it helped me in 2023
    thank you =)
     
  16. AlwaysLupey

    AlwaysLupey

    Joined:
    Jul 29, 2023
    Posts:
    1
    I have been wrapping my head looking for the answer to this in the Documentation and the answer was so simple!! Thank you. You're a life saver.