Simple way to check if tags exist then do something?

I have the following code which basically destroies game objects on mouse clicks, I want to firstly destroy the game objects with the tag “1” then if there are no more game objects with the tag “1” start destroying the game objects with the tag “2”. I tried to check with an if statement if there are still “1” tagged game objects present but it gives me an error saying it cant convert the gameobject[ ] to a bool which kinda makes sense so Im looking for another, hopefully simple way to check.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class No2CubeHP : MonoBehaviour {
   
    public GameObject two;



    void Update()
    {
       
        two = GameObject.FindGameObjectWithTag ("2");
        // CHECK IF GAME OBJECTS WITH THE TAG "1" EXIST HERE

        if (Input.GetMouseButtonDown (0) && // PUT THE CONDITION THAT GAME OBJECTS WITH TAG "1" DON`T EXIST HERE)
        {
            Destroy (two);
        }


    }


}
1 Like
if(GameObject.FindGameObjectsWithTag("1").Length == 0)
3 Likes

If you know the number of each category, you can just subtract one each time you destroy it and check for zero, then you won’t need to find the GameObject each time.

I’m fairly sure that someone wrote you a script for your question based on, among others, my answer in another thread, too. Sorry, this is your third thread on the topic…

Unless you’ve changed your mind about destroying objects in order, the other script (from that thread) would be better. If certain tagged objects can be instantiated out of order, I still believe that maintaining a list of the differently tagged items would be nicer.

I`m just experimenting with everything, while I highly appreciate the help of everyone I just want to try and understand as many things as possible by finding more than one way to accomplish things , yes the scripts on the past topics were very helpful too.

Thank you very much