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.
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.
This worked for me
ActionBars[] bars = transform.Cast().Where (c => c.gameObject.tag == "yourTagName").Select(c=> c.GetComponent()).ToArray ();