Destroy gameObject with tag

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:

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:

Destroy (GameObject.FindWithTag("gameMenu"));
8 Likes

thanks man, it’s very helpful :slight_smile:

Destroy(gameObjectWithTag(“gameMenu”));
Спасибо ! Это работает для уничтожения старого объекта ( сейчас как раз RPG )типа Diablo делаю, и мне как-раз понадобилось создавать и подсвечивать новую target position и уничтожать старую target position

Tai kad zinok niekas cia taves nesupranta. Ar tu is mezdio iskritai ir nematai, kad cia angliksai raso?

1 Like

How do you destroy a simple cube?

Destroy(this.gameobject);

Thank U my dear, I needed that so much!!

thanks bro for this

How Do i Destroy Every Gam,e Object with The SameTag

I set up a function that can be called from elsewhere:

    void Update ()
    {
        if (condition)
        {
            DestroyWithTag ("myTag");
        }
    }

    void DestroyWithTag (string destroyTag)
    {
        GameObject[] destroyObject;
        destroyObject = GameObject.FindGameObjectsWithTag(destroyTag);
        foreach (GameObject oneObject in destroyObject)
            Destroy (oneObject);
    }
2 Likes

Thanks, man, very useful

Thank you this was very helful

thank you it’s very useful

thank you

u found answer in 2010, it helped me in 2023
thank you =)

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.