How to find child with tag?

Hey guys,

I have a gameobject with 5 children, they all have spriterenderers. I need to access one of them with the tag (Liquid Color) but I can’t seem to find a simple way. It does’t really have to be through tag I suppose, name is fine too.

Manaliquidpriv = GetComponentInChildren<SpriteRenderer>();

Thats selecting all of the spriterenderers in the children, so I’m guessing something close to that? lol

Thanks

1 Like

public class Helper {
public static T FindComponentInChildWithTag(this GameObject parent, string tag)where T:Component{
Transform t = parent.transform;
foreach(Transform tr in t)
{
if(tr.tag == tag)
{
return tr.GetComponent();
}
}
return null;
}
}

and you use like:

SpriteRenderer sp = parentObject.FindComponentInChildWithTag<SpriteRenderer>("LiquidColor");

Edit: what if I want to get all children components with that tag, as we would do with “GetComponentsInChildren”?

public static class Helper
{
    public static T[] FindComponentsInChildrenWithTag<T>(this GameObject parent, string tag, bool forceActive = false) where T : Component
    {
        if(parent == null) { throw new System.ArgumentNullException(); }
        if(string.IsNullOrEmpty(tag) == true) { throw new System.ArgumentNullException(); }
        List<T> list = new List<T>(parent.GetComponentsInChildren<T>(forceActive));
        if(list.Count == 0) { return null; }

        for(int i = list.Count - 1; i >= 0; i--) 
        {
            if (list*.CompareTag(tag) == false)*

{
list.RemoveAt(i);
}
}
return list.ToArray();
}

public static T FindComponentInChildWithTag(this GameObject parent, string tag, bool forceActive = false) where T : Component
{
if (parent == null) { throw new System.ArgumentNullException(); }
if (string.IsNullOrEmpty(tag) == true) { throw new System.ArgumentNullException(); }

T [] list = parent.GetComponentsInChildren(forceActive);
foreach(T t in list)
{
if (t.CompareTag(tag) == true)
{
return list*;*
}
}
return null;
}
}

Hi,
My little contribution.

If you want to find a 1st generation child GameObject by Tag:

public static GameObject FindGameObjectInChildWithTag (GameObject parent, string tag)
	{
		Transform t = parent.transform;

		for (int i = 0; i < t.childCount; i++) 
		{
			if(t.GetChild(i).gameObject.tag == tag)
			{
				return t.GetChild(i).gameObject;
			}
				
		}
			
		return null;
	}

In these situations, I would consider creating a new MonoBehaviour Script and put it on your object you are looking for. You can use empty MonoBehaviours to tag things and then find them with GetComponentsInChildren. This is more flexible than tags and names, because you can have multiple of these components on any object. Actually, by the name “tag” this is what you would intuitively expect tags to be like. Yes, you have to create those scripts, but that’s really simple. You can create as many components as you need, while there is a finite amount of tags for some reason. Also components can hold additional data.
This kinda goes in the direction of DOTS.

Often times you don’t even need to look up those objects when your dedicated component can handle whatever needs to be done to the object.

You can use LINQ:

var renderer = GetComponentsInChildren().Where(r => r.tag == "Your Tag").ToArray()[0];

The most simple one imo is to find it via ParentTransform.find(“NameOfChild”);
for more details, refere to

but i prefer if you use this, you need to do that in the start and cache it for future use.

You can write extension functions for your need.
@Hathakas

For an individual child:

public static Transform GetChildWithTag(this Transform transform, string tag)
        {
            if (transform.childCount == 0)
            {
                return null;
            }

            for (int i = 0; i < transform.childCount; i++)
            {
                if (transform.GetChild(i).CompareTag(tag))
                {
                    return transform;
                }
                else
                {
                    Transform childWithTag = GetChildWithTag(transform.GetChild(i), tag);
                    return childWithTag;
                }
            }

            return null;
        }

For multiple children:

public static List<Transform> GetChildrenWithTag(this Transform transform, string tag, List<Transform> childrenList = null)
        {
            if (transform.childCount == 0)
            {
                return null;
            }

            if (childrenList == null)
            {
                childrenList = new List<Transform>();
            }

            for (int i = 0; i < transform.childCount; i++)
            {
                if (transform.GetChild(i).CompareTag(tag))
                {
                    childrenList.Add(transform);
                }
                GetChildrenWithTag(transform.GetChild(i), tag, childrenList);
            }

            return childrenList;
        }