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!

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
            }
        }
    }

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);
            }
        }
    }
}

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));

There’s no ready made function for that. You have to write it yourself.

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*.transform.parent == transform.parent)*

{
for (int e = 0; e < localWayPoints.Length; i++)
{
localWayPoints[e] = allWayPonts*;*
}
}
}

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