- Home /
Is it possible to FindWithTag only within children of a certain gameObject?
In my game, certain objects my player interacts with have 4 empty spheres around them (all children of the object), which I've tagged as "Zone"s. Because there are many of these objects, it seems as I can't use FindGameObjectsWithTag to find an instance's 'Zones', because it will return ALL objects instances' zones. So I ask, is it possible to use this method within an instance of an object?
Returning all children of the object by looping through them is not a possible solution, as it has many other children with different tags.
What about the main objects, are those tagged or named the same?
The main objects are tagged differently and also named differently. I got the answer I was looking for thanks to Jessy's answer!
Answer by Jessy · Feb 16, 2011 at 08:36 PM
No, you can't do what you're asking directly, but I don't see why looping is a problem.
foreach (Transform child in parentTransform) if (child.CompareTag("Zone")) {}
// Put whatever you want in the brackets.
Why is that not appropriate?
Awesome! CompareTag is exactly what I needed. I'll loop through all of them and the ones with the tag "Zone" are the ones I'll use to do what I want. Thank you very much!
Certainly! ;-) There is another way to compare tags, but it's not as good thanks Eric! http://answers.unity3d.com/questions/34589/why-does-comparetag-exist
Jessy's answer is the best one I could find so far. I've updated it for my version of Unity 5.5.
"parentTransform" doesn't seem to work for me. Below is my hack.
public Transform _myGameObject;
void Awake()
{
//This finds the transform for the child gameobject
foreach (Transform child in transform)
if (child.CompareTag ("tagSomethingSomething")) {
_myGameObject = child;
}
}
Sharing is Carin' IndieNendoroid https://twitter.com/IndieNendoroid
Your answer
Follow this Question
Related Questions
Find GameObject of tag "x" no matter where it is? 2 Answers
Better solution for GameObject.FindGameObjectsWithTag 2 Answers
Getcomponent error? 1 Answer
Best method to manage multiple tags 2 Answers