- Home /
How to GameObject.FindGameObjectsWithTag within children of a specific GameObject?
I want to get a collection (List or GameObject[]... whatever I can iterate through) of the children inside a specific GameObject with a Tag assigned to them. But I'm only able to run FindGameObjectsWithTag in a global scope... grabbing all GO's with the desired Tag string.
// What the documentation and examples out there says:
GameObject[] actors= GameObject.FindGameObjectsWithTag("Actor");
// What I would like to be able to do:
GameObject[] actors= actorsHolder.FindGameObjectsWithTag("Actor");
Thanks in advance people.
Peace out!
Sorry misread the question:
Usually, and kind of 'Find' function is going to ask Unity to do some heavy lifting in order to find what you need. And in a general sense, its always good to save references for objects you intend to work with (like the GameObject[] actors array you set up for yourself.) If for any reason you need to find a subclass of objects inside a set of objects that you already have. You are prolly going to have to (Like suggested by allen3) find a way to sort thru them as needed. If you need that subset often, make another array. References are cheap, use them recklessly.
List<GameObject> childObject;
void Awake()
{
childObject.AddRange(GameObject.FindGameObjectsWithTag("tag"));
}
Answer by TBruce · Jun 03, 2016 at 05:22 AM
This script will return all children tagged with a specific name. It searches all children recursively.
All you need to do is add it to the parent object, then set the searchTag (tag to search for) in the inspector and run the game. The actors list will be populated with a children that have the search tag specified. (warning case sensitive). An editor script would be required to allow an drop down of actual tags.
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class GameObjectSearcher : MonoBehaviour
{
public string searchTag;
public List<GameObject> actors = new List<GameObject>();
void Start()
{
if (searchTag != null)
{
FindObjectwithTag(searchTag);
}
}
public void FindObjectwithTag(string _tag)
{
actors.Clear();
Transform parent = transform;
GetChildObject(parent, _tag);
}
public void GetChildObject(Transform parent, string _tag)
{
for (int i = 0; i < parent.childCount; i++)
{
Transform child = parent.GetChild(i);
if (child.tag == _tag)
{
actors.Add(child.gameObject);
}
if (child.childCount > 0)
{
GetChildObject(child, _tag);
}
}
}
}
It also does recursive gameobject finding. Thanks for your answer $$anonymous$$avina.
Here is an enhanced version
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class GameObjectSearcher : $$anonymous$$onoBehaviour
{
[HideInInspector]
public string searchTag;
public List<GameObject> actors = new List<GameObject>();
void Start()
{
if (searchTag != null)
{
FindObjectwithTag(searchTag);
}
}
public void FindObjectwithTag(string _tag)
{
actors.Clear();
Transform parent = transform;
GetChildObject(parent, _tag);
}
public void GetChildObject(Transform parent, string _tag)
{
for (int i = 0; i < parent.childCount; i++)
{
Transform child = parent.GetChild(i);
if (child.tag == _tag)
{
actors.Add(child.gameObject);
}
if (child.childCount > 0)
{
GetChildObject(child, _tag);
}
}
}
}
and place the following in the Editor folder
using UnityEngine;
using System;
using UnityEditor;
[CustomEditor(typeof(GameObjectSearcher))]
[System.Serializable]
public class GameObjectSearcherCustomEditor: Editor
{
public override void OnInspectorGUI()
{
var target_cs = (GameObjectSearcher)target;
DrawDefaultInspector();
GameObjectSearcher gameObjectSearcherScript = target_cs;
if(!Application.isPlaying)
{
string tagStr = gameObjectSearcherScript.searchTag;
tagStr = EditorGUILayout.TagField("Search Tag", tagStr);
if (tagStr != gameObjectSearcherScript.searchTag)
{
gameObjectSearcherScript.searchTag = tagStr;
}
}
}
}
Answer by thunderdev321 · May 12, 2017 at 05:26 PM
Here is an extension method I made
public static class TransformExtensions
{
public static List<GameObject> FindObjectsWithTag(this Transform parent, string tag)
{
List<GameObject> taggedGameObjects = new List<GameObject>();
for (int i = 0; i < parent.childCount; i++)
{
Transform child = parent.GetChild(i);
if (child.tag == tag)
{
taggedGameObjects.Add(child.gameObject);
}
if (child.childCount > 0)
{
taggedGameObjects.AddRange(FindObjectsWithTag(child, tag));
}
}
return taggedGameObjects;
}
}
Usage
public class MyScript : MonoBehaviour
{
public void Start()
{
var myGameObject = transform.FindObjectsWithTag("myTag").FirstOrDefault();
if (myGameObject != null)
{
// do something
}
}
}
Answer by ZayLong · Dec 27, 2018 at 02:32 AM
Here's a one line solution. It assumes the script is attached to the parent. It runs FindGameObjectsWithTag. Converts that to a List. Then runs a Lambda expression to iterate through that list and find the GameObject whos parent is the GameObject that is calling the script.
GameObject found = new List<GameObject>(GameObject.FindGameObjectsWithTag("tagToSearchFor")).Find(g => g.transform.IsChildOf( this.transform));
Answer by allenallenallen · Jun 03, 2016 at 03:46 AM
http://answers.unity3d.com/questions/285133/find-child-of-a-game-object-using-tag.html
http://answers.unity3d.com/questions/47989/is-it-possible-to-findwithtag-only-within-children.html
There's no ready made function for that. You have to write it yourself.
You are right. I accepted $$anonymous$$avina's answer as the correct one. Because it does what my question was asking. I was just hoping for a native method; but yes, I need to put to work my brain hamster every now and then.
Thanks for your links and comment Allena. /bow
Answer by ResistanceGaming · Aug 22, 2017 at 10:30 AM
bit of a late one on this so this answer is for anyone who stumbles accross it, I was having the same issue and found a way around it wthout doing any edito scripts or any thing too complicated. I used the following:
private GameObject[] allWayPoints = GameObject.FindObjectsWithTag("waypoint");
private GameObject[] localWaypoints = new GameObject[transform.parent.childCount];
for (int i = 0; i < allWayPoints.Length; i++)
{
if (allWayPoints[i].transform.parent == transform.parent)
{
for (int e = 0; e < localWayPoints.Length; i++)
{
localWayPoints[e] = allWayPonts[i];
}
}
}
for some context this is part of a code that instatiates an npc character, there are multiple waypoints in my scene but want the npc's to use only therelocal ones that i have mapped out around obstaclerelevant to where they spawn, so I don't want them using all the waypoints in the scene as it would turn into a mess, this way they are instantiated as a child of the spawner for that area and only have access to the waypoints that are child objects of that spawner. Hope this helps or at least gives someone another perspective on this issue
Your answer
![](https://koobas.hobune.stream/wayback/20220612082203im_/https://answers.unity.com/themes/thub/images/avi.jpg)