- Home /
Get object child list with tag
Is there any way to get child list with tag, for example something like that (where Parent is object, from which one I'm trying to get child list with tag):
Parent.FindGameObjectsWithTag("Child");
Its not working, its just example from GameObject.FindGameObjectsWithTag("Respawn")); but I need something similar to it.
Need to get child list in array, that are tagged in one object.
That's a confusing question. First, dont say you need a list, the word actually already mean something (close to ArrayList). So you want an array.
Do you want an array of all the children of an object that have a certain tag, or do you want an array of all the object in the scene that have the tag "Child" ?
"array of all the children of an object that have a certain tag" this is what I'm looking for. I know that usually FindGameObjectsWithTag searching for all object in scene, but I need to find them in a specific object
what do you mean by "Direct children or any descendant"? There is just a couple objects inside object with tag, and I need to find them, because amount of childrens is not static, it can be more than 20.
I mean do you want to find children of children - to which the answer appears to be no?
Answer by whydoidoit · Jun 17, 2012 at 12:09 AM
JS:
import System.Linq;
var arrayOfChildren = parentGameObject.transform.Cast.<Transform>().Where(function(c){ return c.gameObject.tag == "Child";}).ToArray();
C#:
using System.Linq;
var arrayOfChildren = parentGameObject.transform.Cast<Transform>().Where(c=>c.gameObject.tag == "Child").ToArray();
I tell you - it's the best thing I ever did. I mean this stuff is damn handy - and fairly short and hence error resistant, but when you really put it to work, it changes the ways you think about problems. You just have to be careful not to do things too many times because the code looks short, but the iterations could be huge.
I learned most of it thanks to Visual Studio and Resharper doing lots of refactorings of foreach loops until I finally bowed to the pressure and got to grips with the more complex stuff.
You do need C# for some things - anonymous classes being one that come in very handy compared to BooHashes that you get in US. Also US has some weird bugs in things like .ToDictionary() which means you can only use the single parameter version.
You can wrap it in extension method in C#:
using UnityEngine;
using System.Collections;
using System.Linq;
public static class GameObjectExtentions
{
public static GameObject[] FindChildrenWithTag(this GameObject go, string tag)
{
return go.transform.Cast<Transform>().Where(c => c.gameObject.tag == tag).Select(t => t.gameObject).ToArray();
}
}
Then just call somewhere in your code:
GameObject go;
GameObject[] taggedChildren = go.FindChildrenWithTag("$$anonymous$$yTag");
Answer by hawken · Sep 12, 2015 at 07:11 PM
you would probably want to use List for anything run-time, Array can be inflexible.
using System.Collections.Generic;
List<GameObject> gingers;
void ILikeGingers() {
//grab all the kids and only keep the ones with ginger tags
Transform[] allChildren = gameObject.GetComponentsInChildren<Transform>();
foreach (Transform child in allChildren) {
if (child.gameObject.tag == "Ginger") {
gingers.Add(child.gameObject);
}
}
}
Is this the cheapest method to collect a list of children objects with a particular tag?
Your answer
Follow this Question
Related Questions
GameObjectWithTag Child 1 Answer
Find children by tag from Player 1 Answer
Find GameObjects with a certain Script [Solved] 1 Answer
Get a list of children (in order) 1 Answer
Find a child with a name, how to?? 5 Answers