- Home /
Can a tagged object exclude itself from FindGameObjectWithTag?
This has already been asked once before, but as changing tags isn't an option for me, I'm wondering if any less hacky methods have been figured out since then or, failing that, if anybody could clarify how exactly one can use arrays to do what I'm trying to do; which isn't explained very clearly in the other question.
Good question. Probably not, no due to it returning a Boolean or operating on it as a string value, both things you wish to keep the same as the rest i imagine.
Possibly through excluding it by referring to "this" during array for loop etc.... if(this.gameObject.tag){array[i].tag = null}- assu$$anonymous$$g the script dealing with array checks is attached to the said object you wish to exclude.
Hope that helps a little. Gruffy
I would probably just set up a bool or integer variable that you can check on the object you don't want to add. That's assu$$anonymous$$g those tagged objects already have a script.
So when you go through your list of all the tagged objects you could just check if that bool or int variable is correct before adding it to the list.
Sounds like you already have a working solution but using a bool or int variable could be a little more flexible if you want more than just one tagged object not to show up in list.
That way you could have a lot more run time flexibility with adding or removing objects from the list but this may be unnecessary for your project.
Answer by Simon-Larsen · Mar 25, 2014 at 12:16 AM
Going off of Gruffy's idea. I excluded the gameObject of which this script is attached, but added all the other gameObjects of a certain tag to a List
using UnityEngine;
using System.Collections.Generic;
public class FindTaggedGameObjects : MonoBehaviour {
List<GameObject> gos = new List<GameObject>();
void Start () {
foreach (GameObject go in GameObject.FindGameObjectsWithTag ("YourTag")) {
if (go.Equals(this.gameObject))
continue;
gos.Add(go);
}
}
}
...and here's how to do it with two arrays :
//get an array of all objects tagged as "Tag"
GameObject[] all = GameObject.FindGameObjectsWithTag("Tag") ;
//create a new array, 1 less in length since this object won't be included
GameObject[] others = new GameObject[all.Length-1] ;
//loop thru and assign gameObjects from [all] to [others] so long as it isnt this one
int index = 0 ;
for(int i = 0 ; i < all.Length ; i++)
{
if(all[i] == this.gameObject)
continue ;
others[index] = all[i] ;
index ++ ;
}
The same as Simons, but with pretty LINQ:
using UnityEngine;
using System.Collections.Generic;
using System.Linq;
public class test : $$anonymous$$onoBehaviour
{
List<GameObject> gos = new List<GameObject>();
void Start ()
{
gos = GameObject.FindGameObjectsWithTag("YourTag").Where(x => !x.Equals(this.gameObject)).ToList();
}
}
Halle-motherfunking-LLUJAH! This is funking excellent. There should be like an archive of this kind of clever workaround.
Answer by force5386 · Mar 25, 2014 at 12:44 AM
One idea:
GameObject[] obj=FindGameObjectsWithTag('Itsyourtagyeah')as GameObject;
GameObject[] nobj=new GameObject[obj.length-1];
int i=0;
foreach(Gameobject o in obj){
if(o!=this.gameobject){nobj[i]=o; i++;}
}
It should work.
Answer by LukasLicek · Dec 21, 2017 at 04:19 PM
My javascript solution (changing a tag temporarily though) would be :
var people : GameObject[];
var person : GameObject;
function Update(){
this.gameObject.tag = "Counter"; //temporary tag
people = GameObject.FindGameObjectsWithTag("Human"); //find everything
Debug.Log("Found "+people.length+" people.");
this.gameObject.tag = "Human"; //change your tag back
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613141101im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Tagged objects not returning value 0 Answers
Multiple Cars not working 1 Answer
Using a Parameterized arraylist (C#)??? 1 Answer
Distribute terrain in zones 3 Answers
How to make a drag/drop script only affect objects with a tag? 1 Answer