Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
2
Question by Smireles · Jun 03, 2016 at 03:32 AM · c#gameobject.findgameobject.tag

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!

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image ninja_gear · Jun 03, 2016 at 04:46 AM 1
Share

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.

avatar image Plsk1n_ · Apr 20, 2020 at 08:29 AM 0
Share
     List<GameObject> childObject;
     
    void Awake()
     { 
        
         childObject.AddRange(GameObject.FindGameObjectsWithTag("tag"));
         
     }

5 Replies

· Add your reply
  • Sort: 
avatar image
6
Best Answer

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);
             }
         }
     }
 }
Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Smireles · Jun 03, 2016 at 06:26 AM 0
Share

It also does recursive gameobject finding. Thanks for your answer $$anonymous$$avina.

avatar image TBruce Smireles · Jun 03, 2016 at 07:24 PM 0
Share

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;
             }
         }
     }
 }
avatar image
9

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
             }
         }
     }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
4

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

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Smireles · Jun 03, 2016 at 06:28 AM 0
Share

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

avatar image
0

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

157 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Is "GameObject.FindAllGameObjectsWithTag" related to the hierachy? 1 Answer

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Locating proper gameObject to which player has to jump 1 Answer

Renderer on object disabled after level reload 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges