- Home /
The question is answered, right answer was accepted
Find Attached GameObjects in an array using tag?
EDIT: Problem solved, thank you very much guys!
I have a script that I'm trying to edit for multiplayer. Finding any old object with a tag will find every player's objects, so I need a solution to find just the objects attached to the player.
What I'm looking for is a solution like"Gameobject[] temp = transform.parent.FindWithTags("ActionBars");
But it appears that won't work, because you can't access transforms like that.
So I am guessing I'll need a foreach loop, but I'm terrible with that function.
Answer by zharik86 · Aug 30, 2014 at 08:08 AM
Solution like:
Gameobject[] temp = transform.parent.FindChildrenWithTags("ActionBars");
The simplest way, it to create recurrent function for search of all children in a tree. And when finding necessary to enter it in the list. The code sample is lower:
private List<Transform> actionbars = new List<Transform>(); //our list for all object with correct tag
void Start() {
actionbars.Clear(); //clear list
this.myFindChild(this.transform, "ActionBars"); //find all objects with tag "ActionBars"
}
//recurrent functions
public void myFindChild(Transform tpParent, string tpTag) {
for(int i = 0; i < tpParent.childCount; i++) { //See all child and see they child
if (tpParent.GetChild(i).tag == tpTag) { //check tag of child
actionbars.Add(tpParent.GetChild(i));
}
//See childs of current child
this.myFindChild(tpParent.GetChild(i), tpTag);
}
}
I hope that it will help you.
Thanks so much, that's exactly what I was looking for. Seems way over my head though. Where did you learn this?
I'm now also having trouble with my next line of code that gets each of the spells inside the transform, as it wants a gameObject.
Here's the code I'm working with:
bool hoverActionbar = false;
foreach (ActionBars ab in actionbars) {
if (ab.hoverActionBar)
hoverActionbar = true;
}
@Addyarb Where I learned it there it already isn't present:) In my example you find all Transform objects with tag "ActionBars". Then you shall use some other search for your cycle:
bool hoverActionbar = false;
foreach (Transform ab in actionbars) {
if (ab.GetComponent<ActionBars>().hoverActionBar) {
hoverActionbar = true;
//If only one variable changes, after its change there is no need to view the list further therefore it is possible to break a cycle
break;
}
}
Is there any way I can send these scripts for you to look at? I have 3 that reference each other a lot, and it's hard to explain the problem that I'm having, and the character limit is too low to post the scripts. I wouldn't $$anonymous$$d paying you if it's legal to solicit that here! If not I can post the scripts somewhere else and link to them?
Sorry to bring up an old question...I'm having a hard time figuring out how to access the child. Like the script is finding the child of the parent but how do I access it? I can't set a variable to it. I need to access the child's sprite. (I'm refering to the first answer: private List actionbars = new List();)
Answer by Testudo · Mar 23, 2015 at 08:44 PM
This worked for me
ActionBars[] bars = transform.Cast().Where (c => c.gameObject.tag == "yourTagName").Select(c=> c.GetComponent()).ToArray ();
Follow this Question
Related Questions
Unity networking tutorial? 6 Answers
GameObject.FindGameObjectsWithTag problems 2 Answers
How to have player camera Render enemy but not itself with same tag and camera clipping mask? 0 Answers
How to spawn an animal at each transform of a gameobject with a tag position in another scene? 0 Answers
How to setup a local multiplayer game? 0 Answers